add a new MCInstPrinter class, move the (trivial) MCDisassmbler ctor inline.
[llvm/avr.git] / lib / Transforms / Utils / ValueMapper.cpp
blob2d8332f5252ab9592b9946cb19e49dfcaaa46258
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/ValueMapper.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
18 #include "llvm/Constants.h"
19 #include "llvm/GlobalValue.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Metadata.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/ErrorHandling.h"
25 using namespace llvm;
27 Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) {
28 Value *&VMSlot = VM[V];
29 if (VMSlot) return VMSlot; // Does it exist in the map yet?
31 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
32 // DenseMap. This includes any recursive calls to MapValue.
34 // Global values and metadata do not need to be seeded into the ValueMap if
35 // they are using the identity mapping.
36 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
37 return VMSlot = const_cast<Value*>(V);
39 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
40 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
41 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
42 isa<UndefValue>(C) || isa<MDString>(C))
43 return VMSlot = C; // Primitive constants map directly
44 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
45 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
46 i != e; ++i) {
47 Value *MV = MapValue(*i, VM, Context);
48 if (MV != *i) {
49 // This array must contain a reference to a global, make a new array
50 // and return it.
52 std::vector<Constant*> Values;
53 Values.reserve(CA->getNumOperands());
54 for (User::op_iterator j = b; j != i; ++j)
55 Values.push_back(cast<Constant>(*j));
56 Values.push_back(cast<Constant>(MV));
57 for (++i; i != e; ++i)
58 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
59 return VM[V] = ConstantArray::get(CA->getType(), Values);
62 return VM[V] = C;
64 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
65 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
66 i != e; ++i) {
67 Value *MV = MapValue(*i, VM, Context);
68 if (MV != *i) {
69 // This struct must contain a reference to a global, make a new struct
70 // and return it.
72 std::vector<Constant*> Values;
73 Values.reserve(CS->getNumOperands());
74 for (User::op_iterator j = b; j != i; ++j)
75 Values.push_back(cast<Constant>(*j));
76 Values.push_back(cast<Constant>(MV));
77 for (++i; i != e; ++i)
78 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
79 return VM[V] = ConstantStruct::get(CS->getType(), Values);
82 return VM[V] = C;
84 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
85 std::vector<Constant*> Ops;
86 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
87 Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
88 return VM[V] = CE->getWithOperands(Ops);
89 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
90 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
91 i != e; ++i) {
92 Value *MV = MapValue(*i, VM, Context);
93 if (MV != *i) {
94 // This vector value must contain a reference to a global, make a new
95 // vector constant and return it.
97 std::vector<Constant*> Values;
98 Values.reserve(CP->getNumOperands());
99 for (User::op_iterator j = b; j != i; ++j)
100 Values.push_back(cast<Constant>(*j));
101 Values.push_back(cast<Constant>(MV));
102 for (++i; i != e; ++i)
103 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
104 return VM[V] = ConstantVector::get(Values);
107 return VM[V] = C;
109 } else {
110 llvm_unreachable("Unknown type of constant!");
113 return 0;
116 /// RemapInstruction - Convert the instruction operands from referencing the
117 /// current values into those specified by ValueMap.
119 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
120 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
121 Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
122 assert(V && "Referenced value not in value map!");
123 *op = V;