blob: 30fc60081d5919f7b441713ec0d5784a8703ae94 [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"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000016#include "llvm/Constants.h"
Chris Lattner1bb95912009-10-29 00:31:02 +000017#include "llvm/Function.h"
Jay Foad95c3e482011-06-23 09:09:15 +000018#include "llvm/Instructions.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000019#include "llvm/Metadata.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000020using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000021
Chris Lattner1afcace2011-07-09 17:41:24 +000022// Out of line method to get vtable etc for class.
23void ValueMapTypeRemapper::Anchor() {}
24
25Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
26 ValueMapTypeRemapper *TypeMapper) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000027 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner5e665f52007-02-03 00:08:31 +000028
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000029 // If the value already exists in the map, use it.
30 if (I != VM.end() && I->second) return I->second;
31
Dan Gohman6cb8c232010-08-26 15:41:53 +000032 // Global values do not need to be seeded into the VM if they
33 // are using the identity mapping.
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000034 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
35 return VM[V] = const_cast<Value*>(V);
Chris Lattner5f92e2b2003-10-06 15:23:43 +000036
Chris Lattner1b3ef342010-01-21 21:05:54 +000037 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000038 // If this is a module-level metadata and we know that nothing at the module
39 // level is changing, then use an identity mapping.
40 if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
41 return VM[V] = const_cast<Value*>(V);
42
Chris Lattner51e62f02011-01-24 03:18:24 +000043 // Create a dummy node in case we have a metadata cycle.
Jay Foadec9186b2011-04-21 19:59:31 +000044 MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>());
Chris Lattner51e62f02011-01-24 03:18:24 +000045 VM[V] = Dummy;
46
Dan Gohman6cb8c232010-08-26 15:41:53 +000047 // Check all operands to see if any need to be remapped.
48 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
49 Value *OP = MD->getOperand(i);
Chris Lattner1afcace2011-07-09 17:41:24 +000050 if (OP == 0 || MapValue(OP, VM, Flags, TypeMapper) == OP) continue;
Dan Gohman6cb8c232010-08-26 15:41:53 +000051
Chris Lattner51e62f02011-01-24 03:18:24 +000052 // Ok, at least one operand needs remapping.
Dan Gohman6cb8c232010-08-26 15:41:53 +000053 SmallVector<Value*, 4> Elts;
54 Elts.reserve(MD->getNumOperands());
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000055 for (i = 0; i != e; ++i) {
56 Value *Op = MD->getOperand(i);
Chris Lattner1afcace2011-07-09 17:41:24 +000057 Elts.push_back(Op ? MapValue(Op, VM, Flags, TypeMapper) : 0);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000058 }
Jay Foadec9186b2011-04-21 19:59:31 +000059 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dan Gohman6cb8c232010-08-26 15:41:53 +000060 Dummy->replaceAllUsesWith(NewMD);
Chris Lattner51e62f02011-01-24 03:18:24 +000061 VM[V] = NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000062 MDNode::deleteTemporary(Dummy);
Chris Lattner51e62f02011-01-24 03:18:24 +000063 return NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000064 }
65
Chris Lattner51e62f02011-01-24 03:18:24 +000066 VM[V] = const_cast<Value*>(V);
67 MDNode::deleteTemporary(Dummy);
68
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000069 // No operands needed remapping. Use an identity mapping.
Chris Lattner51e62f02011-01-24 03:18:24 +000070 return const_cast<Value*>(V);
Victor Hernandezf58c34d2010-01-20 05:49:59 +000071 }
72
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000073 // Okay, this either must be a constant (which may or may not be mappable) or
74 // is something that is not in the mapping table.
Chris Lattner77488cc2009-10-29 00:28:30 +000075 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Dan Gohman6cb8c232010-08-26 15:41:53 +000076 if (C == 0)
77 return 0;
Chris Lattner77488cc2009-10-29 00:28:30 +000078
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000079 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattner1afcace2011-07-09 17:41:24 +000080 Function *F =
81 cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000082 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
Chris Lattner1afcace2011-07-09 17:41:24 +000083 Flags, TypeMapper));
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000084 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000085 }
Chris Lattner77488cc2009-10-29 00:28:30 +000086
Chris Lattner1afcace2011-07-09 17:41:24 +000087 // Otherwise, we have some other constant to remap. Start by checking to see
88 // if all operands have an identity remapping.
89 unsigned OpNo = 0, NumOperands = C->getNumOperands();
90 Value *Mapped = 0;
91 for (; OpNo != NumOperands; ++OpNo) {
92 Value *Op = C->getOperand(OpNo);
93 Mapped = MapValue(Op, VM, Flags, TypeMapper);
94 if (Mapped != C) break;
Chris Lattner77488cc2009-10-29 00:28:30 +000095 }
Chris Lattner1afcace2011-07-09 17:41:24 +000096
97 // See if the type mapper wants to remap the type as well.
98 Type *NewTy = C->getType();
99 if (TypeMapper)
100 NewTy = TypeMapper->remapType(NewTy);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000101
Chris Lattner1afcace2011-07-09 17:41:24 +0000102 // If the result type and all operands match up, then just insert an identity
103 // mapping.
104 if (OpNo == NumOperands && NewTy == C->getType())
105 return VM[V] = C;
106
107 // Okay, we need to create a new constant. We've already processed some or
108 // all of the operands, set them all up now.
109 SmallVector<Constant*, 8> Ops;
110 Ops.reserve(NumOperands);
111 for (unsigned j = 0; j != OpNo; ++j)
112 Ops.push_back(cast<Constant>(C->getOperand(j)));
113
114 // If one of the operands mismatch, push it and the other mapped operands.
115 if (OpNo != NumOperands) {
116 Ops.push_back(cast<Constant>(Mapped));
117
118 // Map the rest of the operands that aren't processed yet.
119 for (++OpNo; OpNo != NumOperands; ++OpNo)
120 Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
121 Flags, TypeMapper));
122 }
123
124 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
125 return VM[V] = CE->getWithOperands(Ops, NewTy);
126 if (isa<ConstantArray>(C))
127 return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
128 if (isa<ConstantStruct>(C))
129 return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
130 if (isa<ConstantVector>(C))
131 return VM[V] = ConstantVector::get(Ops);
132 // If this is a no-operand constant, it must be because the type was remapped.
133 if (isa<UndefValue>(C))
134 return VM[V] = UndefValue::get(NewTy);
135 if (isa<ConstantAggregateZero>(C))
136 return VM[V] = ConstantAggregateZero::get(NewTy);
137 assert(isa<ConstantPointerNull>(C));
138 return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000139}
Brian Gaeke6129af32004-05-19 09:08:12 +0000140
141/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000142/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000143///
Dan Gohman6cb8c232010-08-26 15:41:53 +0000144void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
Chris Lattner1afcace2011-07-09 17:41:24 +0000145 RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){
Dan Gohman6cb8c232010-08-26 15:41:53 +0000146 // Remap operands.
Gabor Greifba2c4872008-05-30 21:24:22 +0000147 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000148 Value *V = MapValue(*op, VMap, Flags, TypeMapper);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000149 // If we aren't ignoring missing entries, assert that something happened.
150 if (V != 0)
151 *op = V;
152 else
153 assert((Flags & RF_IgnoreMissingEntries) &&
154 "Referenced value not in value map!");
Brian Gaeke6129af32004-05-19 09:08:12 +0000155 }
Daniel Dunbarfd406f12010-08-26 03:48:08 +0000156
Jay Foad95c3e482011-06-23 09:09:15 +0000157 // Remap phi nodes' incoming blocks.
158 if (PHINode *PN = dyn_cast<PHINode>(I)) {
159 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
160 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
161 // If we aren't ignoring missing entries, assert that something happened.
162 if (V != 0)
163 PN->setIncomingBlock(i, cast<BasicBlock>(V));
164 else
165 assert((Flags & RF_IgnoreMissingEntries) &&
166 "Referenced block not in value map!");
167 }
168 }
169
Chris Lattnerf5464532011-07-14 21:25:42 +0000170 // Remap attached metadata.
Dan Gohman6cb8c232010-08-26 15:41:53 +0000171 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Chris Lattnerf5464532011-07-14 21:25:42 +0000172 I->getAllMetadata(MDs);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000173 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
174 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000175 MDNode *Old = MI->second;
176 MDNode *New = MapValue(Old, VMap, Flags, TypeMapper);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000177 if (New != Old)
Chris Lattner1afcace2011-07-09 17:41:24 +0000178 I->setMetadata(MI->first, New);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000179 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000180
181 // If the instruction's type is being remapped, do so now.
182 if (TypeMapper)
183 I->mutateType(TypeMapper->remapType(I->getType()));
Dan Gohman6cb8c232010-08-26 15:41:53 +0000184}