blob: 0f20e6df6c965a79fd4e6e5fc5122e821ba60d64 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner51cbcbf2002-11-20 20:47:41 +00009//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
Dan Gohman05ea54e2010-08-24 18:50:07 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/InlineAsm.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Metadata.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022
Chris Lattner1afcace2011-07-09 17:41:24 +000023// Out of line method to get vtable etc for class.
Craig Topper4bb51cc2012-09-26 06:36:36 +000024void ValueMapTypeRemapper::anchor() {}
James Molloya84a83b2013-05-28 15:17:05 +000025void ValueMaterializer::anchor() {}
Chris Lattner1afcace2011-07-09 17:41:24 +000026
27Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
James Molloya84a83b2013-05-28 15:17:05 +000028 ValueMapTypeRemapper *TypeMapper,
29 ValueMaterializer *Materializer) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000030 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner5e665f52007-02-03 00:08:31 +000031
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000032 // If the value already exists in the map, use it.
33 if (I != VM.end() && I->second) return I->second;
34
James Molloya84a83b2013-05-28 15:17:05 +000035 // If we have a materializer and it can materialize a value, use that.
36 if (Materializer) {
37 if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
38 return VM[V] = NewV;
39 }
40
Dan Gohman6cb8c232010-08-26 15:41:53 +000041 // Global values do not need to be seeded into the VM if they
42 // are using the identity mapping.
Chris Lattner7305c552011-07-15 23:18:40 +000043 if (isa<GlobalValue>(V) || isa<MDString>(V))
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000044 return VM[V] = const_cast<Value*>(V);
Chris Lattner7305c552011-07-15 23:18:40 +000045
46 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
47 // Inline asm may need *type* remapping.
48 FunctionType *NewTy = IA->getFunctionType();
49 if (TypeMapper) {
50 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
51
52 if (NewTy != IA->getFunctionType())
53 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
54 IA->hasSideEffects(), IA->isAlignStack());
55 }
56
57 return VM[V] = const_cast<Value*>(V);
58 }
59
Chris Lattner5f92e2b2003-10-06 15:23:43 +000060
Chris Lattner1b3ef342010-01-21 21:05:54 +000061 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000062 // If this is a module-level metadata and we know that nothing at the module
63 // level is changing, then use an identity mapping.
64 if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
65 return VM[V] = const_cast<Value*>(V);
66
Chris Lattner51e62f02011-01-24 03:18:24 +000067 // Create a dummy node in case we have a metadata cycle.
Dmitri Gribenko5c332db2013-05-05 00:40:33 +000068 MDNode *Dummy = MDNode::getTemporary(V->getContext(), None);
Chris Lattner51e62f02011-01-24 03:18:24 +000069 VM[V] = Dummy;
70
Dan Gohman6cb8c232010-08-26 15:41:53 +000071 // Check all operands to see if any need to be remapped.
72 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
73 Value *OP = MD->getOperand(i);
Stephen Hinesdce4a402014-05-29 02:49:00 -070074 if (!OP) continue;
James Molloya84a83b2013-05-28 15:17:05 +000075 Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
Manman Ren16d1a6b2013-01-31 21:19:18 +000076 // Use identity map if Mapped_Op is null and we can ignore missing
77 // entries.
78 if (Mapped_OP == OP ||
Stephen Hinesdce4a402014-05-29 02:49:00 -070079 (Mapped_OP == nullptr && (Flags & RF_IgnoreMissingEntries)))
Manman Ren16d1a6b2013-01-31 21:19:18 +000080 continue;
Dan Gohman6cb8c232010-08-26 15:41:53 +000081
Chris Lattner51e62f02011-01-24 03:18:24 +000082 // Ok, at least one operand needs remapping.
Dan Gohman6cb8c232010-08-26 15:41:53 +000083 SmallVector<Value*, 4> Elts;
84 Elts.reserve(MD->getNumOperands());
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000085 for (i = 0; i != e; ++i) {
86 Value *Op = MD->getOperand(i);
Stephen Hinesdce4a402014-05-29 02:49:00 -070087 if (!Op)
88 Elts.push_back(nullptr);
Manman Ren16d1a6b2013-01-31 21:19:18 +000089 else {
James Molloya84a83b2013-05-28 15:17:05 +000090 Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Manman Ren16d1a6b2013-01-31 21:19:18 +000091 // Use identity map if Mapped_Op is null and we can ignore missing
92 // entries.
Stephen Hinesdce4a402014-05-29 02:49:00 -070093 if (Mapped_Op == nullptr && (Flags & RF_IgnoreMissingEntries))
Manman Ren16d1a6b2013-01-31 21:19:18 +000094 Mapped_Op = Op;
95 Elts.push_back(Mapped_Op);
96 }
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000097 }
Jay Foadec9186b2011-04-21 19:59:31 +000098 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dan Gohman6cb8c232010-08-26 15:41:53 +000099 Dummy->replaceAllUsesWith(NewMD);
Chris Lattner51e62f02011-01-24 03:18:24 +0000100 VM[V] = NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +0000101 MDNode::deleteTemporary(Dummy);
Chris Lattner51e62f02011-01-24 03:18:24 +0000102 return NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +0000103 }
104
Chris Lattner51e62f02011-01-24 03:18:24 +0000105 VM[V] = const_cast<Value*>(V);
106 MDNode::deleteTemporary(Dummy);
107
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000108 // No operands needed remapping. Use an identity mapping.
Chris Lattner51e62f02011-01-24 03:18:24 +0000109 return const_cast<Value*>(V);
Victor Hernandezf58c34d2010-01-20 05:49:59 +0000110 }
111
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000112 // Okay, this either must be a constant (which may or may not be mappable) or
113 // is something that is not in the mapping table.
Chris Lattner77488cc2009-10-29 00:28:30 +0000114 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700115 if (!C)
116 return nullptr;
Chris Lattner77488cc2009-10-29 00:28:30 +0000117
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000118 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000119 Function *F =
James Molloya84a83b2013-05-28 15:17:05 +0000120 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000121 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
James Molloya84a83b2013-05-28 15:17:05 +0000122 Flags, TypeMapper, Materializer));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000123 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000124 }
Chris Lattner77488cc2009-10-29 00:28:30 +0000125
Chris Lattner1afcace2011-07-09 17:41:24 +0000126 // Otherwise, we have some other constant to remap. Start by checking to see
127 // if all operands have an identity remapping.
128 unsigned OpNo = 0, NumOperands = C->getNumOperands();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700129 Value *Mapped = nullptr;
Chris Lattner1afcace2011-07-09 17:41:24 +0000130 for (; OpNo != NumOperands; ++OpNo) {
131 Value *Op = C->getOperand(OpNo);
James Molloya84a83b2013-05-28 15:17:05 +0000132 Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
Chris Lattner1afcace2011-07-09 17:41:24 +0000133 if (Mapped != C) break;
Chris Lattner77488cc2009-10-29 00:28:30 +0000134 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000135
136 // See if the type mapper wants to remap the type as well.
137 Type *NewTy = C->getType();
138 if (TypeMapper)
139 NewTy = TypeMapper->remapType(NewTy);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000140
Chris Lattner1afcace2011-07-09 17:41:24 +0000141 // If the result type and all operands match up, then just insert an identity
142 // mapping.
143 if (OpNo == NumOperands && NewTy == C->getType())
144 return VM[V] = C;
145
146 // Okay, we need to create a new constant. We've already processed some or
147 // all of the operands, set them all up now.
148 SmallVector<Constant*, 8> Ops;
149 Ops.reserve(NumOperands);
150 for (unsigned j = 0; j != OpNo; ++j)
151 Ops.push_back(cast<Constant>(C->getOperand(j)));
152
153 // If one of the operands mismatch, push it and the other mapped operands.
154 if (OpNo != NumOperands) {
155 Ops.push_back(cast<Constant>(Mapped));
156
157 // Map the rest of the operands that aren't processed yet.
158 for (++OpNo; OpNo != NumOperands; ++OpNo)
159 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
James Molloya84a83b2013-05-28 15:17:05 +0000160 Flags, TypeMapper, Materializer));
Chris Lattner1afcace2011-07-09 17:41:24 +0000161 }
162
163 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
164 return VM[V] = CE->getWithOperands(Ops, NewTy);
165 if (isa<ConstantArray>(C))
166 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
167 if (isa<ConstantStruct>(C))
168 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
169 if (isa<ConstantVector>(C))
170 return VM[V] = ConstantVector::get(Ops);
171 // If this is a no-operand constant, it must be because the type was remapped.
172 if (isa<UndefValue>(C))
173 return VM[V] = UndefValue::get(NewTy);
174 if (isa<ConstantAggregateZero>(C))
175 return VM[V] = ConstantAggregateZero::get(NewTy);
176 assert(isa<ConstantPointerNull>(C));
177 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000178}
Brian Gaeke6129af32004-05-19 09:08:12 +0000179
180/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000181/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000182///
Dan Gohman6cb8c232010-08-26 15:41:53 +0000183void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
James Molloya84a83b2013-05-28 15:17:05 +0000184 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
185 ValueMaterializer *Materializer){
Dan Gohman6cb8c232010-08-26 15:41:53 +0000186 // Remap operands.
Gabor Greifba2c4872008-05-30 21:24:22 +0000187 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
James Molloya84a83b2013-05-28 15:17:05 +0000188 Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000189 // If we aren't ignoring missing entries, assert that something happened.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700190 if (V)
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000191 *op = V;
192 else
193 assert((Flags & RF_IgnoreMissingEntries) &&
194 "Referenced value not in value map!");
Brian Gaeke6129af32004-05-19 09:08:12 +0000195 }
Daniel Dunbarfd406f12010-08-26 03:48:08 +0000196
Jay Foad95c3e482011-06-23 09:09:15 +0000197 // Remap phi nodes' incoming blocks.
198 if (PHINode *PN = dyn_cast<PHINode>(I)) {
199 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
200 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
201 // If we aren't ignoring missing entries, assert that something happened.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700202 if (V)
Jay Foad95c3e482011-06-23 09:09:15 +0000203 PN->setIncomingBlock(i, cast<BasicBlock>(V));
204 else
205 assert((Flags & RF_IgnoreMissingEntries) &&
206 "Referenced block not in value map!");
207 }
208 }
209
Devang Patel1f35b172011-08-04 20:02:18 +0000210 // Remap attached metadata.
Dan Gohman6cb8c232010-08-26 15:41:53 +0000211 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patel1f35b172011-08-04 20:02:18 +0000212 I->getAllMetadata(MDs);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000213 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
214 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000215 MDNode *Old = MI->second;
James Molloya84a83b2013-05-28 15:17:05 +0000216 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper, Materializer);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000217 if (New != Old)
Chris Lattner1afcace2011-07-09 17:41:24 +0000218 I->setMetadata(MI->first, New);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000219 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000220
221 // If the instruction's type is being remapped, do so now.
222 if (TypeMapper)
223 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohman6cb8c232010-08-26 15:41:53 +0000224}