ValueMapper.cpp revision 3943084136b71934ae62e240c256494b4589584f
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  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 and non-function-local metadata do not need to be seeded into
31  // the ValueMap if they are using the identity mapping.
32  if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
33      (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal()))
34    return VMSlot = const_cast<Value*>(V);
35
36  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
37    SmallVector<Value*, 4> Elts;
38    for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
39      Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
40    return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
41  }
42
43  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
44  if (C == 0) return 0;
45
46  if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
47      isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
48      isa<UndefValue>(C) || isa<MDString>(C))
49    return VMSlot = C;           // Primitive constants map directly
50
51  if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
52    for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
53         i != e; ++i) {
54      Value *MV = MapValue(*i, VM);
55      if (MV != *i) {
56        // This array must contain a reference to a global, make a new array
57        // and return it.
58        //
59        std::vector<Constant*> Values;
60        Values.reserve(CA->getNumOperands());
61        for (User::op_iterator j = b; j != i; ++j)
62          Values.push_back(cast<Constant>(*j));
63        Values.push_back(cast<Constant>(MV));
64        for (++i; i != e; ++i)
65          Values.push_back(cast<Constant>(MapValue(*i, VM)));
66        return VM[V] = ConstantArray::get(CA->getType(), Values);
67      }
68    }
69    return VM[V] = C;
70  }
71
72  if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
73    for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
74         i != e; ++i) {
75      Value *MV = MapValue(*i, VM);
76      if (MV != *i) {
77        // This struct must contain a reference to a global, make a new struct
78        // and return it.
79        //
80        std::vector<Constant*> Values;
81        Values.reserve(CS->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] = ConstantStruct::get(CS->getType(), Values);
88      }
89    }
90    return VM[V] = C;
91  }
92
93  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
94    std::vector<Constant*> Ops;
95    for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
96      Ops.push_back(cast<Constant>(MapValue(*i, VM)));
97    return VM[V] = CE->getWithOperands(Ops);
98  }
99
100  if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
101    for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
102         i != e; ++i) {
103      Value *MV = MapValue(*i, VM);
104      if (MV != *i) {
105        // This vector value must contain a reference to a global, make a new
106        // vector constant and return it.
107        //
108        std::vector<Constant*> Values;
109        Values.reserve(CV->getNumOperands());
110        for (User::op_iterator j = b; j != i; ++j)
111          Values.push_back(cast<Constant>(*j));
112        Values.push_back(cast<Constant>(MV));
113        for (++i; i != e; ++i)
114          Values.push_back(cast<Constant>(MapValue(*i, VM)));
115        return VM[V] = ConstantVector::get(Values);
116      }
117    }
118    return VM[V] = C;
119  }
120
121  BlockAddress *BA = cast<BlockAddress>(C);
122  Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
123  BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
124  return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
125}
126
127/// RemapInstruction - Convert the instruction operands from referencing the
128/// current values into those specified by ValueMap.
129///
130void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &ValueMap) {
131  for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
132    Value *V = MapValue(*op, ValueMap);
133    assert(V && "Referenced value not in value map!");
134    *op = V;
135  }
136}
137
138