ValueMapper.cpp revision ce934260dc4ce12bd59f15aaa5ef28520214ad4f
1//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/ValueMapper.h"
16#include "llvm/Type.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
19#include "llvm/Metadata.h"
20#include "llvm/ADT/SmallVector.h"
21using namespace llvm;
22
23Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
24  Value *&VMSlot = VM[V];
25  if (VMSlot) return VMSlot;      // Does it exist in the map yet?
26
27  // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
28  // DenseMap.  This includes any recursive calls to MapValue.
29
30  // Global values do not need to be seeded into the VM if they
31  // are using the identity mapping.
32  if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
33    return VMSlot = const_cast<Value*>(V);
34
35  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
36    // Start by assuming that we'll use the identity mapping.
37    VMSlot = const_cast<Value*>(V);
38
39    // Check all operands to see if any need to be remapped.
40    for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
41      Value *OP = MD->getOperand(i);
42      if (!OP || MapValue(OP, VM) == OP) continue;
43
44      // Ok, at least one operand needs remapping.
45      MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
46      VM[V] = Dummy;
47      SmallVector<Value*, 4> Elts;
48      Elts.reserve(MD->getNumOperands());
49      for (i = 0; i != e; ++i)
50        Elts.push_back(MD->getOperand(i) ?
51                       MapValue(MD->getOperand(i), VM) : 0);
52      MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
53      Dummy->replaceAllUsesWith(NewMD);
54      MDNode::deleteTemporary(Dummy);
55      return VM[V] = NewMD;
56    }
57
58    // No operands needed remapping; keep the identity map.
59    return const_cast<Value*>(V);
60  }
61
62  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
63  if (C == 0) return 0;
64
65  if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
66      isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
67      isa<UndefValue>(C))
68    return VMSlot = C;           // Primitive constants map directly
69
70  if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
71    for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
72         i != e; ++i) {
73      Value *MV = MapValue(*i, VM);
74      if (MV != *i) {
75        // This array must contain a reference to a global, make a new array
76        // and return it.
77        //
78        std::vector<Constant*> Values;
79        Values.reserve(CA->getNumOperands());
80        for (User::op_iterator j = b; j != i; ++j)
81          Values.push_back(cast<Constant>(*j));
82        Values.push_back(cast<Constant>(MV));
83        for (++i; i != e; ++i)
84          Values.push_back(cast<Constant>(MapValue(*i, VM)));
85        return VM[V] = ConstantArray::get(CA->getType(), Values);
86      }
87    }
88    return VM[V] = C;
89  }
90
91  if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
92    for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
93         i != e; ++i) {
94      Value *MV = MapValue(*i, VM);
95      if (MV != *i) {
96        // This struct must contain a reference to a global, make a new struct
97        // and return it.
98        //
99        std::vector<Constant*> Values;
100        Values.reserve(CS->getNumOperands());
101        for (User::op_iterator j = b; j != i; ++j)
102          Values.push_back(cast<Constant>(*j));
103        Values.push_back(cast<Constant>(MV));
104        for (++i; i != e; ++i)
105          Values.push_back(cast<Constant>(MapValue(*i, VM)));
106        return VM[V] = ConstantStruct::get(CS->getType(), Values);
107      }
108    }
109    return VM[V] = C;
110  }
111
112  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
113    std::vector<Constant*> Ops;
114    for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
115      Ops.push_back(cast<Constant>(MapValue(*i, VM)));
116    return VM[V] = CE->getWithOperands(Ops);
117  }
118
119  if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
120    for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
121         i != e; ++i) {
122      Value *MV = MapValue(*i, VM);
123      if (MV != *i) {
124        // This vector value must contain a reference to a global, make a new
125        // vector constant and return it.
126        //
127        std::vector<Constant*> Values;
128        Values.reserve(CV->getNumOperands());
129        for (User::op_iterator j = b; j != i; ++j)
130          Values.push_back(cast<Constant>(*j));
131        Values.push_back(cast<Constant>(MV));
132        for (++i; i != e; ++i)
133          Values.push_back(cast<Constant>(MapValue(*i, VM)));
134        return VM[V] = ConstantVector::get(Values);
135      }
136    }
137    return VM[V] = C;
138  }
139
140  BlockAddress *BA = cast<BlockAddress>(C);
141  Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
142  BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
143  return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
144}
145
146/// RemapInstruction - Convert the instruction operands from referencing the
147/// current values into those specified by VMap.
148///
149void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
150  // Remap operands.
151  for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
152    Value *V = MapValue(*op, VMap);
153    assert(V && "Referenced value not in value map!");
154    *op = V;
155  }
156
157  // Remap attached metadata.
158  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
159  I->getAllMetadata(MDs);
160  for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
161       MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
162    Value *Old = MI->second;
163    Value *New = MapValue(Old, VMap);
164    if (New != Old)
165      I->setMetadata(MI->first, cast<MDNode>(New));
166  }
167}
168