Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 1 | //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the MapValue function, which is shared by various parts of |
| 11 | // the lib/Transforms/Utils library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Chris Lattner | 7305c55 | 2011-07-15 23:18:40 +0000 | [diff] [blame] | 18 | #include "llvm/InlineAsm.h" |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 20 | #include "llvm/Metadata.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 23 | // Out of line method to get vtable etc for class. |
Craig Topper | 4bb51cc | 2012-09-26 06:36:36 +0000 | [diff] [blame] | 24 | void ValueMapTypeRemapper::anchor() {} |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 25 | |
| 26 | Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags, |
| 27 | ValueMapTypeRemapper *TypeMapper) { |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 28 | ValueToValueMapTy::iterator I = VM.find(V); |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 29 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 30 | // If the value already exists in the map, use it. |
| 31 | if (I != VM.end() && I->second) return I->second; |
| 32 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 33 | // Global values do not need to be seeded into the VM if they |
| 34 | // are using the identity mapping. |
Chris Lattner | 7305c55 | 2011-07-15 23:18:40 +0000 | [diff] [blame] | 35 | if (isa<GlobalValue>(V) || isa<MDString>(V)) |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 36 | return VM[V] = const_cast<Value*>(V); |
Chris Lattner | 7305c55 | 2011-07-15 23:18:40 +0000 | [diff] [blame] | 37 | |
| 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 Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 1b3ef34 | 2010-01-21 21:05:54 +0000 | [diff] [blame] | 53 | if (const MDNode *MD = dyn_cast<MDNode>(V)) { |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 54 | // 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 Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 59 | // Create a dummy node in case we have a metadata cycle. |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 60 | MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>()); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 61 | VM[V] = Dummy; |
| 62 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 63 | // 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); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 66 | if (OP == 0 || MapValue(OP, VM, Flags, TypeMapper) == OP) continue; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 68 | // Ok, at least one operand needs remapping. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 69 | SmallVector<Value*, 4> Elts; |
| 70 | Elts.reserve(MD->getNumOperands()); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 71 | for (i = 0; i != e; ++i) { |
| 72 | Value *Op = MD->getOperand(i); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 73 | Elts.push_back(Op ? MapValue(Op, VM, Flags, TypeMapper) : 0); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 74 | } |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 75 | MDNode *NewMD = MDNode::get(V->getContext(), Elts); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 76 | Dummy->replaceAllUsesWith(NewMD); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 77 | VM[V] = NewMD; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 78 | MDNode::deleteTemporary(Dummy); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 79 | return NewMD; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 82 | VM[V] = const_cast<Value*>(V); |
| 83 | MDNode::deleteTemporary(Dummy); |
| 84 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 85 | // No operands needed remapping. Use an identity mapping. |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 86 | return const_cast<Value*>(V); |
Victor Hernandez | f58c34d | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 89 | // Okay, this either must be a constant (which may or may not be mappable) or |
| 90 | // is something that is not in the mapping table. |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 91 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 92 | if (C == 0) |
| 93 | return 0; |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 94 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 95 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 96 | Function *F = |
| 97 | cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper)); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 98 | BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM, |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 99 | Flags, TypeMapper)); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 100 | return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 103 | // Otherwise, we have some other constant to remap. Start by checking to see |
| 104 | // if all operands have an identity remapping. |
| 105 | unsigned OpNo = 0, NumOperands = C->getNumOperands(); |
| 106 | Value *Mapped = 0; |
| 107 | for (; OpNo != NumOperands; ++OpNo) { |
| 108 | Value *Op = C->getOperand(OpNo); |
| 109 | Mapped = MapValue(Op, VM, Flags, TypeMapper); |
| 110 | if (Mapped != C) break; |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 112 | |
| 113 | // See if the type mapper wants to remap the type as well. |
| 114 | Type *NewTy = C->getType(); |
| 115 | if (TypeMapper) |
| 116 | NewTy = TypeMapper->remapType(NewTy); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 118 | // If the result type and all operands match up, then just insert an identity |
| 119 | // mapping. |
| 120 | if (OpNo == NumOperands && NewTy == C->getType()) |
| 121 | return VM[V] = C; |
| 122 | |
| 123 | // Okay, we need to create a new constant. We've already processed some or |
| 124 | // all of the operands, set them all up now. |
| 125 | SmallVector<Constant*, 8> Ops; |
| 126 | Ops.reserve(NumOperands); |
| 127 | for (unsigned j = 0; j != OpNo; ++j) |
| 128 | Ops.push_back(cast<Constant>(C->getOperand(j))); |
| 129 | |
| 130 | // If one of the operands mismatch, push it and the other mapped operands. |
| 131 | if (OpNo != NumOperands) { |
| 132 | Ops.push_back(cast<Constant>(Mapped)); |
| 133 | |
| 134 | // Map the rest of the operands that aren't processed yet. |
| 135 | for (++OpNo; OpNo != NumOperands; ++OpNo) |
| 136 | Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM, |
| 137 | Flags, TypeMapper)); |
| 138 | } |
| 139 | |
| 140 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 141 | return VM[V] = CE->getWithOperands(Ops, NewTy); |
| 142 | if (isa<ConstantArray>(C)) |
| 143 | return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops); |
| 144 | if (isa<ConstantStruct>(C)) |
| 145 | return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops); |
| 146 | if (isa<ConstantVector>(C)) |
| 147 | return VM[V] = ConstantVector::get(Ops); |
| 148 | // If this is a no-operand constant, it must be because the type was remapped. |
| 149 | if (isa<UndefValue>(C)) |
| 150 | return VM[V] = UndefValue::get(NewTy); |
| 151 | if (isa<ConstantAggregateZero>(C)) |
| 152 | return VM[V] = ConstantAggregateZero::get(NewTy); |
| 153 | assert(isa<ConstantPointerNull>(C)); |
| 154 | return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy)); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 155 | } |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 156 | |
| 157 | /// RemapInstruction - Convert the instruction operands from referencing the |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 158 | /// current values into those specified by VMap. |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 159 | /// |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 160 | void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 161 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){ |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 162 | // Remap operands. |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 163 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 164 | Value *V = MapValue(*op, VMap, Flags, TypeMapper); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 165 | // If we aren't ignoring missing entries, assert that something happened. |
| 166 | if (V != 0) |
| 167 | *op = V; |
| 168 | else |
| 169 | assert((Flags & RF_IgnoreMissingEntries) && |
| 170 | "Referenced value not in value map!"); |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 171 | } |
Daniel Dunbar | fd406f1 | 2010-08-26 03:48:08 +0000 | [diff] [blame] | 172 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 173 | // Remap phi nodes' incoming blocks. |
| 174 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 175 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 176 | Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags); |
| 177 | // If we aren't ignoring missing entries, assert that something happened. |
| 178 | if (V != 0) |
| 179 | PN->setIncomingBlock(i, cast<BasicBlock>(V)); |
| 180 | else |
| 181 | assert((Flags & RF_IgnoreMissingEntries) && |
| 182 | "Referenced block not in value map!"); |
| 183 | } |
| 184 | } |
| 185 | |
Devang Patel | 1f35b17 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 186 | // Remap attached metadata. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 187 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
Devang Patel | 1f35b17 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 188 | I->getAllMetadata(MDs); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 189 | for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator |
| 190 | MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 191 | MDNode *Old = MI->second; |
| 192 | MDNode *New = MapValue(Old, VMap, Flags, TypeMapper); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 193 | if (New != Old) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 194 | I->setMetadata(MI->first, New); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 196 | |
| 197 | // If the instruction's type is being remapped, do so now. |
| 198 | if (TypeMapper) |
| 199 | I->mutateType(TypeMapper->remapType(I->getType())); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 200 | } |