blob: de6cbdc92d548b0c0f7551b9026b24c5c37c34bf [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 Lattner839a0152010-01-21 21:29:25 +000016#include "llvm/Type.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000017#include "llvm/Constants.h"
Chris Lattner1bb95912009-10-29 00:31:02 +000018#include "llvm/Function.h"
Jay Foad95c3e482011-06-23 09:09:15 +000019#include "llvm/Instructions.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000020#include "llvm/Metadata.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000021#include "llvm/ADT/SmallVector.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000022using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000023
Dan Gohman6cb8c232010-08-26 15:41:53 +000024Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000025 RemapFlags Flags) {
26 ValueToValueMapTy::iterator I = VM.find(V);
Chris Lattner5e665f52007-02-03 00:08:31 +000027
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000028 // If the value already exists in the map, use it.
29 if (I != VM.end() && I->second) return I->second;
30
Dan Gohman6cb8c232010-08-26 15:41:53 +000031 // Global values do not need to be seeded into the VM if they
32 // are using the identity mapping.
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000033 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
34 return VM[V] = const_cast<Value*>(V);
Chris Lattner5f92e2b2003-10-06 15:23:43 +000035
Chris Lattner1b3ef342010-01-21 21:05:54 +000036 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000037 // If this is a module-level metadata and we know that nothing at the module
38 // level is changing, then use an identity mapping.
39 if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
40 return VM[V] = const_cast<Value*>(V);
41
Chris Lattner51e62f02011-01-24 03:18:24 +000042 // Create a dummy node in case we have a metadata cycle.
Jay Foadec9186b2011-04-21 19:59:31 +000043 MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>());
Chris Lattner51e62f02011-01-24 03:18:24 +000044 VM[V] = Dummy;
45
Dan Gohman6cb8c232010-08-26 15:41:53 +000046 // Check all operands to see if any need to be remapped.
47 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
48 Value *OP = MD->getOperand(i);
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000049 if (OP == 0 || MapValue(OP, VM, Flags) == OP) continue;
Dan Gohman6cb8c232010-08-26 15:41:53 +000050
Chris Lattner51e62f02011-01-24 03:18:24 +000051 // Ok, at least one operand needs remapping.
Dan Gohman6cb8c232010-08-26 15:41:53 +000052 SmallVector<Value*, 4> Elts;
53 Elts.reserve(MD->getNumOperands());
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000054 for (i = 0; i != e; ++i) {
55 Value *Op = MD->getOperand(i);
56 Elts.push_back(Op ? MapValue(Op, VM, Flags) : 0);
57 }
Jay Foadec9186b2011-04-21 19:59:31 +000058 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dan Gohman6cb8c232010-08-26 15:41:53 +000059 Dummy->replaceAllUsesWith(NewMD);
Chris Lattner51e62f02011-01-24 03:18:24 +000060 VM[V] = NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000061 MDNode::deleteTemporary(Dummy);
Chris Lattner51e62f02011-01-24 03:18:24 +000062 return NewMD;
Dan Gohman6cb8c232010-08-26 15:41:53 +000063 }
64
Chris Lattner51e62f02011-01-24 03:18:24 +000065 VM[V] = const_cast<Value*>(V);
66 MDNode::deleteTemporary(Dummy);
67
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000068 // No operands needed remapping. Use an identity mapping.
Chris Lattner51e62f02011-01-24 03:18:24 +000069 return const_cast<Value*>(V);
Victor Hernandezf58c34d2010-01-20 05:49:59 +000070 }
71
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000072 // Okay, this either must be a constant (which may or may not be mappable) or
73 // is something that is not in the mapping table.
Chris Lattner77488cc2009-10-29 00:28:30 +000074 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Dan Gohman6cb8c232010-08-26 15:41:53 +000075 if (C == 0)
76 return 0;
Chris Lattner77488cc2009-10-29 00:28:30 +000077
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000078 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
79 Function *F = cast<Function>(MapValue(BA->getFunction(), VM, Flags));
80 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
81 Flags));
82 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000083 }
Chris Lattner77488cc2009-10-29 00:28:30 +000084
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000085 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
86 Value *Op = C->getOperand(i);
87 Value *Mapped = MapValue(Op, VM, Flags);
88 if (Mapped == C) continue;
89
90 // Okay, the operands don't all match. We've already processed some or all
91 // of the operands, set them up now.
Chris Lattner77488cc2009-10-29 00:28:30 +000092 std::vector<Constant*> Ops;
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +000093 Ops.reserve(C->getNumOperands());
94 for (unsigned j = 0; j != i; ++j)
95 Ops.push_back(cast<Constant>(C->getOperand(i)));
96 Ops.push_back(cast<Constant>(Mapped));
97
98 // Map the rest of the operands that aren't processed yet.
99 for (++i; i != e; ++i)
100 Ops.push_back(cast<Constant>(MapValue(C->getOperand(i), VM, Flags)));
101
102 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
103 return VM[V] = CE->getWithOperands(Ops);
104 if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
105 return VM[V] = ConstantArray::get(CA->getType(), Ops);
106 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C))
107 return VM[V] = ConstantStruct::get(CS->getType(), Ops);
108 assert(isa<ConstantVector>(C) && "Unknown mapped constant type");
109 return VM[V] = ConstantVector::get(Ops);
Chris Lattner77488cc2009-10-29 00:28:30 +0000110 }
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000111
112 // If we reach here, all of the operands of the constant match.
113 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000114}
Brian Gaeke6129af32004-05-19 09:08:12 +0000115
116/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000117/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000118///
Dan Gohman6cb8c232010-08-26 15:41:53 +0000119void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000120 RemapFlags Flags) {
Dan Gohman6cb8c232010-08-26 15:41:53 +0000121 // Remap operands.
Gabor Greifba2c4872008-05-30 21:24:22 +0000122 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000123 Value *V = MapValue(*op, VMap, Flags);
124 // If we aren't ignoring missing entries, assert that something happened.
125 if (V != 0)
126 *op = V;
127 else
128 assert((Flags & RF_IgnoreMissingEntries) &&
129 "Referenced value not in value map!");
Brian Gaeke6129af32004-05-19 09:08:12 +0000130 }
Daniel Dunbarfd406f12010-08-26 03:48:08 +0000131
Jay Foad95c3e482011-06-23 09:09:15 +0000132 // Remap phi nodes' incoming blocks.
133 if (PHINode *PN = dyn_cast<PHINode>(I)) {
134 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
135 Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
136 // If we aren't ignoring missing entries, assert that something happened.
137 if (V != 0)
138 PN->setIncomingBlock(i, cast<BasicBlock>(V));
139 else
140 assert((Flags & RF_IgnoreMissingEntries) &&
141 "Referenced block not in value map!");
142 }
143 }
144
Dan Gohman6cb8c232010-08-26 15:41:53 +0000145 // Remap attached metadata.
146 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
147 I->getAllMetadata(MDs);
148 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
149 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
150 Value *Old = MI->second;
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000151 Value *New = MapValue(Old, VMap, Flags);
Dan Gohman6cb8c232010-08-26 15:41:53 +0000152 if (New != Old)
153 I->setMetadata(MI->first, cast<MDNode>(New));
154 }
155}