Add a new LoadAndStorePromoter class, which implements the general
[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 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"
21 using namespace llvm;
22
23 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
24                       RemapFlags Flags) {
25   ValueToValueMapTy::iterator I = VM.find(V);
26   
27   // If the value already exists in the map, use it.
28   if (I != VM.end() && I->second) return I->second;
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 VM[V] = const_cast<Value*>(V);
34
35   if (const MDNode *MD = dyn_cast<MDNode>(V)) {
36     // If this is a module-level metadata and we know that nothing at the module
37     // level is changing, then use an identity mapping.
38     if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
39       return VM[V] = const_cast<Value*>(V);
40     
41     // Check all operands to see if any need to be remapped.
42     for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
43       Value *OP = MD->getOperand(i);
44       if (OP == 0 || MapValue(OP, VM, Flags) == OP) continue;
45
46       // Ok, at least one operand needs remapping.  Create a dummy node in case
47       // we have a metadata cycle.
48       MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
49       VM[V] = Dummy;
50       SmallVector<Value*, 4> Elts;
51       Elts.reserve(MD->getNumOperands());
52       for (i = 0; i != e; ++i) {
53         Value *Op = MD->getOperand(i);
54         Elts.push_back(Op ? MapValue(Op, VM, Flags) : 0);
55       }
56       MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
57       Dummy->replaceAllUsesWith(NewMD);
58       MDNode::deleteTemporary(Dummy);
59       return VM[V] = NewMD;
60     }
61
62     // No operands needed remapping.  Use an identity mapping.
63     return VM[V] = const_cast<Value*>(V);
64   }
65
66   // Okay, this either must be a constant (which may or may not be mappable) or
67   // is something that is not in the mapping table.
68   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
69   if (C == 0)
70     return 0;
71   
72   if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
73     Function *F = cast<Function>(MapValue(BA->getFunction(), VM, Flags));
74     BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
75                                                        Flags));
76     return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
77   }
78   
79   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
80     Value *Op = C->getOperand(i);
81     Value *Mapped = MapValue(Op, VM, Flags);
82     if (Mapped == C) continue;
83     
84     // Okay, the operands don't all match.  We've already processed some or all
85     // of the operands, set them up now.
86     std::vector<Constant*> Ops;
87     Ops.reserve(C->getNumOperands());
88     for (unsigned j = 0; j != i; ++j)
89       Ops.push_back(cast<Constant>(C->getOperand(i)));
90     Ops.push_back(cast<Constant>(Mapped));
91     
92     // Map the rest of the operands that aren't processed yet.
93     for (++i; i != e; ++i)
94       Ops.push_back(cast<Constant>(MapValue(C->getOperand(i), VM, Flags)));
95     
96     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
97       return VM[V] = CE->getWithOperands(Ops);
98     if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
99       return VM[V] = ConstantArray::get(CA->getType(), Ops);
100     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C))
101       return VM[V] = ConstantStruct::get(CS->getType(), Ops);
102     assert(isa<ConstantVector>(C) && "Unknown mapped constant type");
103     return VM[V] = ConstantVector::get(Ops);
104   }
105
106   // If we reach here, all of the operands of the constant match.
107   return VM[V] = C;
108 }
109
110 /// RemapInstruction - Convert the instruction operands from referencing the
111 /// current values into those specified by VMap.
112 ///
113 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
114                             RemapFlags Flags) {
115   // Remap operands.
116   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
117     Value *V = MapValue(*op, VMap, Flags);
118     // If we aren't ignoring missing entries, assert that something happened.
119     if (V != 0)
120       *op = V;
121     else
122       assert((Flags & RF_IgnoreMissingEntries) &&
123              "Referenced value not in value map!");
124   }
125
126   // Remap attached metadata.
127   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
128   I->getAllMetadata(MDs);
129   for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
130        MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
131     Value *Old = MI->second;
132     Value *New = MapValue(Old, VMap, Flags);
133     if (New != Old)
134       I->setMetadata(MI->first, cast<MDNode>(New));
135   }
136 }