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 | 839a015 | 2010-01-21 21:29:25 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 19 | #include "llvm/Metadata.h" |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.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 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 23 | Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 24 | RemapFlags Flags) { |
| 25 | ValueToValueMapTy::iterator I = VM.find(V); |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 26 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 27 | // If the value already exists in the map, use it. |
| 28 | if (I != VM.end() && I->second) return I->second; |
| 29 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 30 | // Global values do not need to be seeded into the VM if they |
| 31 | // are using the identity mapping. |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 32 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V)) |
| 33 | return VM[V] = const_cast<Value*>(V); |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 1b3ef34 | 2010-01-21 21:05:54 +0000 | [diff] [blame] | 35 | if (const MDNode *MD = dyn_cast<MDNode>(V)) { |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 36 | // If this is a module-level metadata and we know that nothing at the module |
| 37 | // level is changing, then use an identity mapping. |
| 38 | if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges)) |
| 39 | return VM[V] = const_cast<Value*>(V); |
| 40 | |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 41 | // Create a dummy node in case we have a metadata cycle. |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame^] | 42 | MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>()); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 43 | VM[V] = Dummy; |
| 44 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 45 | // Check all operands to see if any need to be remapped. |
| 46 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) { |
| 47 | Value *OP = MD->getOperand(i); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 48 | if (OP == 0 || MapValue(OP, VM, Flags) == OP) continue; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 50 | // Ok, at least one operand needs remapping. |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 51 | SmallVector<Value*, 4> Elts; |
| 52 | Elts.reserve(MD->getNumOperands()); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 53 | for (i = 0; i != e; ++i) { |
| 54 | Value *Op = MD->getOperand(i); |
| 55 | Elts.push_back(Op ? MapValue(Op, VM, Flags) : 0); |
| 56 | } |
Jay Foad | ec9186b | 2011-04-21 19:59:31 +0000 | [diff] [blame^] | 57 | MDNode *NewMD = MDNode::get(V->getContext(), Elts); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 58 | Dummy->replaceAllUsesWith(NewMD); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 59 | VM[V] = NewMD; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 60 | MDNode::deleteTemporary(Dummy); |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 61 | return NewMD; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 64 | VM[V] = const_cast<Value*>(V); |
| 65 | MDNode::deleteTemporary(Dummy); |
| 66 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 67 | // No operands needed remapping. Use an identity mapping. |
Chris Lattner | 51e62f0 | 2011-01-24 03:18:24 +0000 | [diff] [blame] | 68 | return const_cast<Value*>(V); |
Victor Hernandez | f58c34d | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 71 | // Okay, this either must be a constant (which may or may not be mappable) or |
| 72 | // is something that is not in the mapping table. |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 73 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 74 | if (C == 0) |
| 75 | return 0; |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 76 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 77 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) { |
| 78 | Function *F = cast<Function>(MapValue(BA->getFunction(), VM, Flags)); |
| 79 | BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM, |
| 80 | Flags)); |
| 81 | return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 83 | |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 84 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { |
| 85 | Value *Op = C->getOperand(i); |
| 86 | Value *Mapped = MapValue(Op, VM, Flags); |
| 87 | if (Mapped == C) continue; |
| 88 | |
| 89 | // Okay, the operands don't all match. We've already processed some or all |
| 90 | // of the operands, set them up now. |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 91 | std::vector<Constant*> Ops; |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 92 | Ops.reserve(C->getNumOperands()); |
| 93 | for (unsigned j = 0; j != i; ++j) |
| 94 | Ops.push_back(cast<Constant>(C->getOperand(i))); |
| 95 | Ops.push_back(cast<Constant>(Mapped)); |
| 96 | |
| 97 | // Map the rest of the operands that aren't processed yet. |
| 98 | for (++i; i != e; ++i) |
| 99 | Ops.push_back(cast<Constant>(MapValue(C->getOperand(i), VM, Flags))); |
| 100 | |
| 101 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 102 | return VM[V] = CE->getWithOperands(Ops); |
| 103 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 104 | return VM[V] = ConstantArray::get(CA->getType(), Ops); |
| 105 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) |
| 106 | return VM[V] = ConstantStruct::get(CS->getType(), Ops); |
| 107 | assert(isa<ConstantVector>(C) && "Unknown mapped constant type"); |
| 108 | return VM[V] = ConstantVector::get(Ops); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 110 | |
| 111 | // If we reach here, all of the operands of the constant match. |
| 112 | return VM[V] = C; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 113 | } |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 114 | |
| 115 | /// RemapInstruction - Convert the instruction operands from referencing the |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 116 | /// current values into those specified by VMap. |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 117 | /// |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 118 | void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 119 | RemapFlags Flags) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 120 | // Remap operands. |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 121 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 122 | Value *V = MapValue(*op, VMap, Flags); |
| 123 | // If we aren't ignoring missing entries, assert that something happened. |
| 124 | if (V != 0) |
| 125 | *op = V; |
| 126 | else |
| 127 | assert((Flags & RF_IgnoreMissingEntries) && |
| 128 | "Referenced value not in value map!"); |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 129 | } |
Daniel Dunbar | fd406f1 | 2010-08-26 03:48:08 +0000 | [diff] [blame] | 130 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 131 | // Remap attached metadata. |
| 132 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
| 133 | I->getAllMetadata(MDs); |
| 134 | for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator |
| 135 | MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) { |
| 136 | Value *Old = MI->second; |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 137 | Value *New = MapValue(Old, VMap, Flags); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 138 | if (New != Old) |
| 139 | I->setMetadata(MI->first, cast<MDNode>(New)); |
| 140 | } |
| 141 | } |