blob: 544c5eed7e0523b5810080fc27e3697f22c5c397 [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() {}
Chris Lattner1afcace2011-07-09 17:41:24 +000025
26Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
27 ValueMapTypeRemapper *TypeMapper) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000028 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner5e665f52007-02-03 00:08:31 +000029
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000030 // If the value already exists in the map, use it.
31 if (I != VM.end() && I->second) return I->second;
32
Dan Gohman6cb8c232010-08-26 15:41:53 +000033 // Global values do not need to be seeded into the VM if they
34 // are using the identity mapping.
Chris Lattner7305c552011-07-15 23:18:40 +000035 if (isa<GlobalValue>(V) || isa<MDString>(V))
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000036 return VM[V] = const_cast<Value*>(V);
Chris Lattner7305c552011-07-15 23:18:40 +000037
38 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
39 // Inline asm may need *type* remapping.
40 FunctionType *NewTy = IA->getFunctionType();
41 if (TypeMapper) {
42 NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
43
44 if (NewTy != IA->getFunctionType())
45 V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
46 IA->hasSideEffects(), IA->isAlignStack());
47 }
48
49 return VM[V] = const_cast<Value*>(V);
50 }
51
Chris Lattner5f92e2b2003-10-06 15:23:43 +000052
Chris Lattner1b3ef342010-01-21 21:05:54 +000053 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000054 // If this is a module-level metadata and we know that nothing at the module
55 // level is changing, then use an identity mapping.
56 if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
57 return VM[V] = const_cast<Value*>(V);
58
Chris Lattner51e62f02011-01-24 03:18:24 +000059 // Create a dummy node in case we have a metadata cycle.
Dmitri Gribenko5c332db2013-05-05 00:40:33 +000060 MDNode *Dummy = MDNode::getTemporary(V->getContext(), None);
Chris Lattner51e62f02011-01-24 03:18:24 +000061 VM[V] = Dummy;
62
Dan Gohman6cb8c232010-08-26 15:41:53 +000063 // Check all operands to see if any need to be remapped.
64 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
65 Value *OP = MD->getOperand(i);
Manman Ren16d1a6b2013-01-31 21:19:18 +000066 if (OP == 0) continue;
67 Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper);
68 // Use identity map if Mapped_Op is null and we can ignore missing
69 // entries.
70 if (Mapped_OP == OP ||
71 (Mapped_OP == 0 && (Flags & RF_IgnoreMissingEntries)))
72 continue;
Dan Gohman6cb8c232010-08-26 15:41:53 +000073
Chris Lattner51e62f02011-01-24 03:18:24 +000074 // Ok, at least one operand needs remapping.
Dan Gohman6cb8c232010-08-26 15:41:53 +000075 SmallVector<Value*, 4> Elts;
76 Elts.reserve(MD->getNumOperands());
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000077 for (i = 0; i != e; ++i) {
78 Value *Op = MD->getOperand(i);
Manman Ren16d1a6b2013-01-31 21:19:18 +000079 if (Op == 0)
80 Elts.push_back(0);
81 else {
82 Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper);
83 // Use identity map if Mapped_Op is null and we can ignore missing
84 // entries.
85 if (Mapped_Op == 0 && (Flags & RF_IgnoreMissingEntries))
86 Mapped_Op = Op;
87 Elts.push_back(Mapped_Op);
88 }
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000089 }
Jay Foadec9186b2011-04-21 19:59:31 +000090 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dan Gohman6cb8c232010-08-26 15:41:53 +000091 Dummy->replaceAllUsesWith(NewMD);
Chris Lattner51e62f02011-01-24 03:18:24 +000092 VM[V] = NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000093 MDNode::deleteTemporary(Dummy);
Chris Lattner51e62f02011-01-24 03:18:24 +000094 return NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000095 }
96
Chris Lattner51e62f02011-01-24 03:18:24 +000097 VM[V] = const_cast<Value*>(V);
98 MDNode::deleteTemporary(Dummy);
99
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000100 // No operands needed remapping. Use an identity mapping.
Chris Lattner51e62f02011-01-24 03:18:24 +0000101 return const_cast<Value*>(V);
Victor Hernandezf58c34d2010-01-20 05:49:59 +0000102 }
103
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000104 // Okay, this either must be a constant (which may or may not be mappable) or
105 // is something that is not in the mapping table.
Chris Lattner77488cc2009-10-29 00:28:30 +0000106 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Dan Gohman6cb8c232010-08-26 15:41:53 +0000107 if (C == 0)
108 return 0;
Chris Lattner77488cc2009-10-29 00:28:30 +0000109
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000110 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000111 Function *F =
112 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000113 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
Chris Lattner1afcace2011-07-09 17:41:24 +0000114 Flags, TypeMapper));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000115 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000116 }
Chris Lattner77488cc2009-10-29 00:28:30 +0000117
Chris Lattner1afcace2011-07-09 17:41:24 +0000118 // Otherwise, we have some other constant to remap. Start by checking to see
119 // if all operands have an identity remapping.
120 unsigned OpNo = 0, NumOperands = C->getNumOperands();
121 Value *Mapped = 0;
122 for (; OpNo != NumOperands; ++OpNo) {
123 Value *Op = C->getOperand(OpNo);
124 Mapped = MapValue(Op, VM, Flags, TypeMapper);
125 if (Mapped != C) break;
Chris Lattner77488cc2009-10-29 00:28:30 +0000126 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000127
128 // See if the type mapper wants to remap the type as well.
129 Type *NewTy = C->getType();
130 if (TypeMapper)
131 NewTy = TypeMapper->remapType(NewTy);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000132
Chris Lattner1afcace2011-07-09 17:41:24 +0000133 // If the result type and all operands match up, then just insert an identity
134 // mapping.
135 if (OpNo == NumOperands && NewTy == C->getType())
136 return VM[V] = C;
137
138 // Okay, we need to create a new constant. We've already processed some or
139 // all of the operands, set them all up now.
140 SmallVector<Constant*, 8> Ops;
141 Ops.reserve(NumOperands);
142 for (unsigned j = 0; j != OpNo; ++j)
143 Ops.push_back(cast<Constant>(C->getOperand(j)));
144
145 // If one of the operands mismatch, push it and the other mapped operands.
146 if (OpNo != NumOperands) {
147 Ops.push_back(cast<Constant>(Mapped));
148
149 // Map the rest of the operands that aren't processed yet.
150 for (++OpNo; OpNo != NumOperands; ++OpNo)
151 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
152 Flags, TypeMapper));
153 }
154
155 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
156 return VM[V] = CE->getWithOperands(Ops, NewTy);
157 if (isa<ConstantArray>(C))
158 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
159 if (isa<ConstantStruct>(C))
160 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
161 if (isa<ConstantVector>(C))
162 return VM[V] = ConstantVector::get(Ops);
163 // If this is a no-operand constant, it must be because the type was remapped.
164 if (isa<UndefValue>(C))
165 return VM[V] = UndefValue::get(NewTy);
166 if (isa<ConstantAggregateZero>(C))
167 return VM[V] = ConstantAggregateZero::get(NewTy);
168 assert(isa<ConstantPointerNull>(C));
169 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000170}
Brian Gaeke6129af32004-05-19 09:08:12 +0000171
172/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000173/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000174///
Dan Gohman6cb8c232010-08-26 15:41:53 +0000175void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
Chris Lattner1afcace2011-07-09 17:41:24 +0000176 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){
Dan Gohman6cb8c232010-08-26 15:41:53 +0000177 // Remap operands.
Gabor Greifba2c4872008-05-30 21:24:22 +0000178 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000179 Value *V = MapValue(*op, VMap, Flags, TypeMapper);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000180 // If we aren't ignoring missing entries, assert that something happened.
181 if (V != 0)
182 *op = V;
183 else
184 assert((Flags & RF_IgnoreMissingEntries) &&
185 "Referenced value not in value map!");
Brian Gaeke6129af32004-05-19 09:08:12 +0000186 }
Daniel Dunbarfd406f12010-08-26 03:48:08 +0000187
Jay Foad95c3e482011-06-23 09:09:15 +0000188 // Remap phi nodes' incoming blocks.
189 if (PHINode *PN = dyn_cast<PHINode>(I)) {
190 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
191 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
192 // If we aren't ignoring missing entries, assert that something happened.
193 if (V != 0)
194 PN->setIncomingBlock(i, cast<BasicBlock>(V));
195 else
196 assert((Flags & RF_IgnoreMissingEntries) &&
197 "Referenced block not in value map!");
198 }
199 }
200
Devang Patel1f35b172011-08-04 20:02:18 +0000201 // Remap attached metadata.
Dan Gohman6cb8c232010-08-26 15:41:53 +0000202 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Devang Patel1f35b172011-08-04 20:02:18 +0000203 I->getAllMetadata(MDs);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000204 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
205 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000206 MDNode *Old = MI->second;
207 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000208 if (New != Old)
Chris Lattner1afcace2011-07-09 17:41:24 +0000209 I->setMetadata(MI->first, New);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000210 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000211
212 // If the instruction's type is being remapped, do so now.
213 if (TypeMapper)
214 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohman6cb8c232010-08-26 15:41:53 +0000215}