Implement a new method, CloneAndPruneFunctionInto, as documented.
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Constants.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Instruction.h"
19 using namespace llvm;
20
21 Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
22   Value *&VMSlot = VM[V];
23   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
24
25   // Global values do not need to be seeded into the ValueMap if they are using
26   // the identity mapping.
27   if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
28     return VMSlot = const_cast<Value*>(V);
29
30   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
31     if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
32         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
33         isa<UndefValue>(C))
34       return VMSlot = C;           // Primitive constants map directly
35     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
36       for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
37         Value *MV = MapValue(CA->getOperand(i), VM);
38         if (MV != CA->getOperand(i)) {
39           // This array must contain a reference to a global, make a new array
40           // and return it.
41           //
42           std::vector<Constant*> Values;
43           Values.reserve(CA->getNumOperands());
44           for (unsigned j = 0; j != i; ++j)
45             Values.push_back(CA->getOperand(j));
46           Values.push_back(cast<Constant>(MV));
47           for (++i; i != e; ++i)
48             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
49           return VMSlot = ConstantArray::get(CA->getType(), Values);
50         }
51       }
52       return VMSlot = C;
53
54     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
55       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
56         Value *MV = MapValue(CS->getOperand(i), VM);
57         if (MV != CS->getOperand(i)) {
58           // This struct must contain a reference to a global, make a new struct
59           // and return it.
60           //
61           std::vector<Constant*> Values;
62           Values.reserve(CS->getNumOperands());
63           for (unsigned j = 0; j != i; ++j)
64             Values.push_back(CS->getOperand(j));
65           Values.push_back(cast<Constant>(MV));
66           for (++i; i != e; ++i)
67             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
68           return VMSlot = ConstantStruct::get(CS->getType(), Values);
69         }
70       }
71       return VMSlot = C;
72
73     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
74       if (CE->getOpcode() == Instruction::Cast) {
75         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
76         return VMSlot = ConstantExpr::getCast(MV, CE->getType());
77       } else if (CE->getOpcode() == Instruction::GetElementPtr) {
78         std::vector<Constant*> Idx;
79         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
80         for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
81           Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
82         return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
83       } else if (CE->getOpcode() == Instruction::Select) {
84         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
85         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
86         Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
87         return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3);
88       } else if (CE->getOpcode() == Instruction::InsertElement) {
89         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
90         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
91         Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM));
92         return VMSlot = ConstantExpr::getInsertElement(MV1, MV2, MV3);
93       } else if (CE->getOpcode() == Instruction::ExtractElement) {
94         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
95         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
96         return VMSlot = ConstantExpr::getExtractElement(MV1, MV2);
97       } else if (CE->getOpcode() == Instruction::ShuffleVector) {
98         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
99         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
100         Constant *MV3 = cast<Constant>(CE->getOperand(2));
101         return VMSlot = ConstantExpr::getShuffleVector(MV1, MV2, MV3);
102       } else {
103         assert(CE->getNumOperands() == 2 && "Must be binary operator?");
104         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
105         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
106         return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
107       }
108
109     } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
110       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
111         Value *MV = MapValue(CP->getOperand(i), VM);
112         if (MV != CP->getOperand(i)) {
113           // This packed value must contain a reference to a global, make a new
114           // packed constant and return it.
115           //
116           std::vector<Constant*> Values;
117           Values.reserve(CP->getNumOperands());
118           for (unsigned j = 0; j != i; ++j)
119             Values.push_back(CP->getOperand(j));
120           Values.push_back(cast<Constant>(MV));
121           for (++i; i != e; ++i)
122             Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
123           return VMSlot = ConstantPacked::get(Values);
124         }
125       }
126       return VMSlot = C;
127       
128     } else {
129       assert(0 && "Unknown type of constant!");
130     }
131   }
132
133   return 0;
134 }
135
136 /// RemapInstruction - Convert the instruction operands from referencing the
137 /// current values into those specified by ValueMap.
138 ///
139 void llvm::RemapInstruction(Instruction *I,
140                             std::map<const Value *, Value*> &ValueMap) {
141   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
142     const Value *Op = I->getOperand(op);
143     Value *V = MapValue(Op, ValueMap);
144     assert(V && "Referenced value not in value map!");
145     I->setOperand(op, V);
146   }
147 }