ValueMapper.cpp revision 186b3d260643de5db63c4bab15beceb9edaee396
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 "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  ValueToValueMapTy::iterator VMI = VM.find(V);
25  if (VMI != VM.end())
26    return VMI->second;      // Does it exist in the map yet?
27
28  // Global values, metadata strings and inline asm do not need to be seeded into
29  // the ValueMap if they are using the identity mapping.
30  if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V)) {
31    VM.insert(std::make_pair(V, const_cast<Value*>(V)));
32    return const_cast<Value*>(V);
33  }
34
35  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
36    // Insert a place holder in map to handle mdnode cycles.
37    Value *TmpV = MDString::get(V->getContext(),
38                                std::string("llvm.md.clone.tmp." + VM.size()));
39    VM.insert(std::make_pair(V, MDNode::get(V->getContext(), &TmpV, 1)));
40
41    bool ReuseMD = true;
42    SmallVector<Value*, 4> Elts;
43    // If metadata element is mapped to a new value then seed metadata
44    // in the map.
45    for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
46      if (!MD->getOperand(i))
47        Elts.push_back(0);
48      else {
49        Value *MappedOp = MapValue(MD->getOperand(i), VM);
50        if (MappedOp != MD->getOperand(i))
51          ReuseMD = false;
52        Elts.push_back(MappedOp);
53      }
54    }
55    if (ReuseMD) {
56      VM.insert(std::make_pair(V, const_cast<Value*>(V)));
57      return const_cast<Value*>(V);
58    }
59    MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
60    VM.insert(std::make_pair(V, NewMD));
61    return NewMD;
62  }
63
64  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
65  if (C == 0) return 0;
66
67  if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
68      isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
69      isa<UndefValue>(C) || isa<MDString>(C))
70    return VM[V] = C;           // Primitive constants map directly
71
72  if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
73    for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
74         i != e; ++i) {
75      Value *MV = MapValue(*i, VM);
76      if (MV != *i) {
77        // This array must contain a reference to a global, make a new array
78        // and return it.
79        //
80        std::vector<Constant*> Values;
81        Values.reserve(CA->getNumOperands());
82        for (User::op_iterator j = b; j != i; ++j)
83          Values.push_back(cast<Constant>(*j));
84        Values.push_back(cast<Constant>(MV));
85        for (++i; i != e; ++i)
86          Values.push_back(cast<Constant>(MapValue(*i, VM)));
87        return VM[V] = ConstantArray::get(CA->getType(), Values);
88      }
89    }
90    return VM[V] = C;
91  }
92
93  if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
94    for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
95         i != e; ++i) {
96      Value *MV = MapValue(*i, VM);
97      if (MV != *i) {
98        // This struct must contain a reference to a global, make a new struct
99        // and return it.
100        //
101        std::vector<Constant*> Values;
102        Values.reserve(CS->getNumOperands());
103        for (User::op_iterator j = b; j != i; ++j)
104          Values.push_back(cast<Constant>(*j));
105        Values.push_back(cast<Constant>(MV));
106        for (++i; i != e; ++i)
107          Values.push_back(cast<Constant>(MapValue(*i, VM)));
108        return VM[V] = ConstantStruct::get(CS->getType(), Values);
109      }
110    }
111    return VM[V] = C;
112  }
113
114  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
115    std::vector<Constant*> Ops;
116    for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
117      Ops.push_back(cast<Constant>(MapValue(*i, VM)));
118    return VM[V] = CE->getWithOperands(Ops);
119  }
120
121  if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
122    for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
123         i != e; ++i) {
124      Value *MV = MapValue(*i, VM);
125      if (MV != *i) {
126        // This vector value must contain a reference to a global, make a new
127        // vector constant and return it.
128        //
129        std::vector<Constant*> Values;
130        Values.reserve(CV->getNumOperands());
131        for (User::op_iterator j = b; j != i; ++j)
132          Values.push_back(cast<Constant>(*j));
133        Values.push_back(cast<Constant>(MV));
134        for (++i; i != e; ++i)
135          Values.push_back(cast<Constant>(MapValue(*i, VM)));
136        return VM[V] = ConstantVector::get(Values);
137      }
138    }
139    return VM[V] = C;
140  }
141
142  BlockAddress *BA = cast<BlockAddress>(C);
143  Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
144  BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
145  return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
146}
147
148/// RemapInstruction - Convert the instruction operands from referencing the
149/// current values into those specified by ValueMap.
150///
151void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &ValueMap) {
152  for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
153    Value *V = MapValue(*op, ValueMap);
154    assert(V && "Referenced value not in value map!");
155    *op = V;
156  }
157}
158
159