llvm.org GIT mirror llvm / 29d3dd8
Cosmetic change. Do not use "ValueMap" as a name for a local variable or an argument. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106698 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 10 years ago
7 changed file(s) with 116 addition(s) and 116 deletion(s). Raw diff Collapse all Expand all
4545 /// CloneModule - Return an exact copy of the specified module
4646 ///
4747 Module *CloneModule(const Module *M);
48 Module *CloneModule(const Module *M, DenseMap &ValueMap);
48 Module *CloneModule(const Module *M, DenseMap &VMap);
4949
5050 /// ClonedCodeInfo - This struct can be used to capture information about code
5151 /// being cloned, while it is being cloned.
8888 /// incoming edges.
8989 ///
9090 /// The correlation between instructions in the source and result basic blocks
91 /// is recorded in the ValueMap map.
91 /// is recorded in the VMap map.
9292 ///
9393 /// If you have a particular suffix you'd like to use to add to any cloned
9494 /// names, specify it as the optional third parameter.
101101 /// parameter.
102102 ///
103103 BasicBlock *CloneBasicBlock(const BasicBlock *BB,
104 DenseMap &ValueMap,
104 DenseMap &VMap,
105105 const Twine &NameSuffix = "", Function *F = 0,
106106 ClonedCodeInfo *CodeInfo = 0);
107107
108108
109109 /// CloneLoop - Clone Loop. Clone dominator info for loop insiders. Populate
110 /// ValueMap using old blocks to new blocks mapping.
110 /// VMap using old blocks to new blocks mapping.
111111 Loop *CloneLoop(Loop *L, LPPassManager *LPM, LoopInfo *LI,
112 DenseMap &ValueMap, Pass *P);
112 DenseMap &VMap, Pass *P);
113113
114114 /// CloneFunction - Return a copy of the specified function, but without
115115 /// embedding the function into another module. Also, any references specified
116 /// in the ValueMap are changed to refer to their mapped value instead of the
117 /// original one. If any of the arguments to the function are in the ValueMap,
118 /// the arguments are deleted from the resultant function. The ValueMap is
116 /// in the VMap are changed to refer to their mapped value instead of the
117 /// original one. If any of the arguments to the function are in the VMap,
118 /// the arguments are deleted from the resultant function. The VMap is
119119 /// updated to include mappings from all of the instructions and basicblocks in
120120 /// the function from their old to new values. The final argument captures
121121 /// information about the cloned code if non-null.
122122 ///
123123 Function *CloneFunction(const Function *F,
124 DenseMap &ValueMap,
124 DenseMap &VMap,
125125 ClonedCodeInfo *CodeInfo = 0);
126126
127 /// CloneFunction - Version of the function that doesn't need the ValueMap.
127 /// CloneFunction - Version of the function that doesn't need the VMap.
128128 ///
129129 inline Function *CloneFunction(const Function *F, ClonedCodeInfo *CodeInfo = 0){
130 DenseMap ValueMap;
131 return CloneFunction(F, ValueMap, CodeInfo);
130 DenseMap VMap;
131 return CloneFunction(F, VMap, CodeInfo);
132132 }
133133
134134 /// Clone OldFunc into NewFunc, transforming the old arguments into references
138138 /// specified suffix to all values cloned.
139139 ///
140140 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
141 DenseMap &ValueMap,
141 DenseMap &VMap,
142142 SmallVectorImpl &Returns,
143143 const char *NameSuffix = "",
144144 ClonedCodeInfo *CodeInfo = 0);
151151 /// dead. Since this doesn't produce an exactly copy of the input, it can't be
152152 /// used for things like CloneFunction or CloneModule.
153153 void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
154 DenseMap &ValueMap,
154 DenseMap &VMap,
155155 SmallVectorImpl &Returns,
156156 const char *NameSuffix = "",
157157 ClonedCodeInfo *CodeInfo = 0,
3131
3232 // CloneBasicBlock - See comments in Cloning.h
3333 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
34 DenseMap &ValueMap,
34 DenseMap &VMap,
3535 const Twine &NameSuffix, Function *F,
3636 ClonedCodeInfo *CodeInfo) {
3737 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
4646 if (II->hasName())
4747 NewInst->setName(II->getName()+NameSuffix);
4848 NewBB->getInstList().push_back(NewInst);
49 ValueMap[II] = NewInst; // Add instruction map to value.
49 VMap[II] = NewInst; // Add instruction map to value.
5050
5151 hasCalls |= (isa(II) && !isa(II));
5252 if (const AllocaInst *AI = dyn_cast(II)) {
7171 // ArgMap values.
7272 //
7373 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
74 DenseMap &ValueMap,
74 DenseMap &VMap,
7575 SmallVectorImpl &Returns,
7676 const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
7777 assert(NameSuffix && "NameSuffix cannot be null!");
7979 #ifndef NDEBUG
8080 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
8181 E = OldFunc->arg_end(); I != E; ++I)
82 assert(ValueMap.count(I) && "No mapping from source argument specified!");
82 assert(VMap.count(I) && "No mapping from source argument specified!");
8383 #endif
8484
8585 // Clone any attributes.
8686 if (NewFunc->arg_size() == OldFunc->arg_size())
8787 NewFunc->copyAttributesFrom(OldFunc);
8888 else {
89 //Some arguments were deleted with the ValueMap. Copy arguments one by one
89 //Some arguments were deleted with the VMap. Copy arguments one by one
9090 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
9191 E = OldFunc->arg_end(); I != E; ++I)
92 if (Argument* Anew = dyn_cast(ValueMap[I]))
92 if (Argument* Anew = dyn_cast(VMap[I]))
9393 Anew->addAttr( OldFunc->getAttributes()
9494 .getParamAttributes(I->getArgNo() + 1));
9595 NewFunc->setAttributes(NewFunc->getAttributes()
110110 const BasicBlock &BB = *BI;
111111
112112 // Create a new basic block and copy instructions into it!
113 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
113 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc,
114114 CodeInfo);
115 ValueMap[&BB] = CBB; // Add basic block mapping.
115 VMap[&BB] = CBB; // Add basic block mapping.
116116
117117 if (ReturnInst *RI = dyn_cast(CBB->getTerminator()))
118118 Returns.push_back(RI);
119119 }
120120
121121 // Loop over all of the instructions in the function, fixing up operand
122 // references as we go. This uses ValueMap to do all the hard work.
122 // references as we go. This uses VMap to do all the hard work.
123123 //
124 for (Function::iterator BB = cast(ValueMap[OldFunc->begin()]),
124 for (Function::iterator BB = cast(VMap[OldFunc->begin()]),
125125 BE = NewFunc->end(); BB != BE; ++BB)
126126 // Loop over all instructions, fixing each one as we find it...
127127 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
128 RemapInstruction(II, ValueMap);
128 RemapInstruction(II, VMap);
129129 }
130130
131131 /// CloneFunction - Return a copy of the specified function, but without
132132 /// embedding the function into another module. Also, any references specified
133 /// in the ValueMap are changed to refer to their mapped value instead of the
134 /// original one. If any of the arguments to the function are in the ValueMap,
135 /// the arguments are deleted from the resultant function. The ValueMap is
133 /// in the VMap are changed to refer to their mapped value instead of the
134 /// original one. If any of the arguments to the function are in the VMap,
135 /// the arguments are deleted from the resultant function. The VMap is
136136 /// updated to include mappings from all of the instructions and basicblocks in
137137 /// the function from their old to new values.
138138 ///
139139 Function *llvm::CloneFunction(const Function *F,
140 DenseMap &ValueMap,
140 DenseMap &VMap,
141141 ClonedCodeInfo *CodeInfo) {
142142 std::vector ArgTypes;
143143
144144 // The user might be deleting arguments to the function by specifying them in
145 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
145 // the VMap. If so, we need to not add the arguments to the arg ty vector
146146 //
147147 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
148148 I != E; ++I)
149 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
149 if (VMap.count(I) == 0) // Haven't mapped the argument to anything yet?
150150 ArgTypes.push_back(I->getType());
151151
152152 // Create a new function type...
160160 Function::arg_iterator DestI = NewF->arg_begin();
161161 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
162162 I != E; ++I)
163 if (ValueMap.count(I) == 0) { // Is this argument preserved?
163 if (VMap.count(I) == 0) { // Is this argument preserved?
164164 DestI->setName(I->getName()); // Copy the name over...
165 ValueMap[I] = DestI++; // Add mapping to ValueMap
165 VMap[I] = DestI++; // Add mapping to VMap
166166 }
167167
168168 SmallVector Returns; // Ignore returns cloned.
169 CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);
169 CloneFunctionInto(NewF, F, VMap, Returns, "", CodeInfo);
170170 return NewF;
171171 }
172172
178178 struct PruningFunctionCloner {
179179 Function *NewFunc;
180180 const Function *OldFunc;
181 DenseMap &ValueMap;
181 DenseMap &VMap;
182182 SmallVectorImpl &Returns;
183183 const char *NameSuffix;
184184 ClonedCodeInfo *CodeInfo;
190190 const char *nameSuffix,
191191 ClonedCodeInfo *codeInfo,
192192 const TargetData *td)
193 : NewFunc(newFunc), OldFunc(oldFunc), ValueMap(valueMap), Returns(returns),
193 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), Returns(returns),
194194 NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) {
195195 }
196196
201201
202202 public:
203203 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
204 /// mapping its operands through ValueMap if they are available.
204 /// mapping its operands through VMap if they are available.
205205 Constant *ConstantFoldMappedInstruction(const Instruction *I);
206206 };
207207 }
210210 /// anything that it can reach.
211211 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
212212 std::vector &ToClone){
213 Value *&BBEntry = ValueMap[BB];
213 Value *&BBEntry = VMap[BB];
214214
215215 // Have we already cloned this block?
216216 if (BBEntry) return;
229229 // If this instruction constant folds, don't bother cloning the instruction,
230230 // instead, just add the constant to the value map.
231231 if (Constant *C = ConstantFoldMappedInstruction(II)) {
232 ValueMap[II] = C;
232 VMap[II] = C;
233233 continue;
234234 }
235235
237237 if (II->hasName())
238238 NewInst->setName(II->getName()+NameSuffix);
239239 NewBB->getInstList().push_back(NewInst);
240 ValueMap[II] = NewInst; // Add instruction map to value.
240 VMap[II] = NewInst; // Add instruction map to value.
241241
242242 hasCalls |= (isa(II) && !isa(II));
243243 if (const AllocaInst *AI = dyn_cast(II)) {
257257 ConstantInt *Cond = dyn_cast(BI->getCondition());
258258 // Or is a known constant in the caller...
259259 if (Cond == 0)
260 Cond = dyn_cast_or_null(ValueMap[BI->getCondition()]);
260 Cond = dyn_cast_or_null(VMap[BI->getCondition()]);
261261
262262 // Constant fold to uncond branch!
263263 if (Cond) {
264264 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
265 ValueMap[OldTI] = BranchInst::Create(Dest, NewBB);
265 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
266266 ToClone.push_back(Dest);
267267 TerminatorDone = true;
268268 }
271271 // If switching on a value known constant in the caller.
272272 ConstantInt *Cond = dyn_cast(SI->getCondition());
273273 if (Cond == 0) // Or known constant after constant prop in the callee...
274 Cond = dyn_cast_or_null(ValueMap[SI->getCondition()]);
274 Cond = dyn_cast_or_null(VMap[SI->getCondition()]);
275275 if (Cond) { // Constant fold to uncond branch!
276276 BasicBlock *Dest = SI->getSuccessor(SI->findCaseValue(Cond));
277 ValueMap[OldTI] = BranchInst::Create(Dest, NewBB);
277 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
278278 ToClone.push_back(Dest);
279279 TerminatorDone = true;
280280 }
285285 if (OldTI->hasName())
286286 NewInst->setName(OldTI->getName()+NameSuffix);
287287 NewBB->getInstList().push_back(NewInst);
288 ValueMap[OldTI] = NewInst; // Add instruction map to value.
288 VMap[OldTI] = NewInst; // Add instruction map to value.
289289
290290 // Recursively clone any reachable successor blocks.
291291 const TerminatorInst *TI = BB->getTerminator();
306306 }
307307
308308 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
309 /// mapping its operands through ValueMap if they are available.
309 /// mapping its operands through VMap if they are available.
310310 Constant *PruningFunctionCloner::
311311 ConstantFoldMappedInstruction(const Instruction *I) {
312312 SmallVector Ops;
313313 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
314314 if (Constant *Op = dyn_cast_or_null(MapValue(I->getOperand(i),
315 ValueMap)))
315 VMap)))
316316 Ops.push_back(Op);
317317 else
318318 return 0; // All operands not constant!
362362 /// dead. Since this doesn't produce an exact copy of the input, it can't be
363363 /// used for things like CloneFunction or CloneModule.
364364 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
365 DenseMap &ValueMap,
365 DenseMap &VMap,
366366 SmallVectorImpl &Returns,
367367 const char *NameSuffix,
368368 ClonedCodeInfo *CodeInfo,
373373 #ifndef NDEBUG
374374 for (Function::const_arg_iterator II = OldFunc->arg_begin(),
375375 E = OldFunc->arg_end(); II != E; ++II)
376 assert(ValueMap.count(II) && "No mapping from source argument specified!");
376 assert(VMap.count(II) && "No mapping from source argument specified!");
377377 #endif
378378
379 PruningFunctionCloner PFC(NewFunc, OldFunc, ValueMap, Returns,
379 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, Returns,
380380 NameSuffix, CodeInfo, TD);
381381
382382 // Clone the entry block, and anything recursively reachable from it.
396396 SmallVector PHIToResolve;
397397 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
398398 BI != BE; ++BI) {
399 BasicBlock *NewBB = cast_or_null(ValueMap[BI]);
399 BasicBlock *NewBB = cast_or_null(VMap[BI]);
400400 if (NewBB == 0) continue; // Dead block.
401401
402402 // Add the new block to the new function.
403403 NewFunc->getBasicBlockList().push_back(NewBB);
404404
405405 // Loop over all of the instructions in the block, fixing up operand
406 // references as we go. This uses ValueMap to do all the hard work.
406 // references as we go. This uses VMap to do all the hard work.
407407 //
408408 BasicBlock::iterator I = NewBB->begin();
409409
454454 I->setMetadata(DbgKind, 0);
455455 }
456456 }
457 RemapInstruction(I, ValueMap);
457 RemapInstruction(I, VMap);
458458 }
459459 }
460460
464464 const PHINode *OPN = PHIToResolve[phino];
465465 unsigned NumPreds = OPN->getNumIncomingValues();
466466 const BasicBlock *OldBB = OPN->getParent();
467 BasicBlock *NewBB = cast(ValueMap[OldBB]);
467 BasicBlock *NewBB = cast(VMap[OldBB]);
468468
469469 // Map operands for blocks that are live and remove operands for blocks
470470 // that are dead.
471471 for (; phino != PHIToResolve.size() &&
472472 PHIToResolve[phino]->getParent() == OldBB; ++phino) {
473473 OPN = PHIToResolve[phino];
474 PHINode *PN = cast(ValueMap[OPN]);
474 PHINode *PN = cast(VMap[OPN]);
475475 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
476476 if (BasicBlock *MappedBlock =
477 cast_or_null(ValueMap[PN->getIncomingBlock(pred)])) {
477 cast_or_null(VMap[PN->getIncomingBlock(pred)])) {
478478 Value *InVal = MapValue(PN->getIncomingValue(pred),
479 ValueMap);
479 VMap);
480480 assert(InVal && "Unknown input value?");
481481 PN->setIncomingValue(pred, InVal);
482482 PN->setIncomingBlock(pred, MappedBlock);
530530 while ((PN = dyn_cast(I++))) {
531531 Value *NV = UndefValue::get(PN->getType());
532532 PN->replaceAllUsesWith(NV);
533 assert(ValueMap[OldI] == PN && "ValueMap mismatch");
534 ValueMap[OldI] = NV;
533 assert(VMap[OldI] == PN && "VMap mismatch");
534 VMap[OldI] = NV;
535535 PN->eraseFromParent();
536536 ++OldI;
537537 }
538538 }
539539 // NOTE: We cannot eliminate single entry phi nodes here, because of
540 // ValueMap. Single entry phi nodes can have multiple ValueMap entries
541 // pointing at them. Thus, deleting one would require scanning the ValueMap
540 // VMap. Single entry phi nodes can have multiple VMap entries
541 // pointing at them. Thus, deleting one would require scanning the VMap
542542 // to update any entries in it that would require that. This would be
543543 // really slow.
544544 }
547547 // and zap unconditional fall-through branches. This happen all the time when
548548 // specializing code: code specialization turns conditional branches into
549549 // uncond branches, and this code folds them.
550 Function::iterator I = cast(ValueMap[&OldFunc->getEntryBlock()]);
550 Function::iterator I = cast(VMap[&OldFunc->getEntryBlock()]);
551551 while (I != NewFunc->end()) {
552552 BranchInst *BI = dyn_cast(I->getTerminator());
553553 if (!BI || BI->isConditional()) { ++I; continue; }
554554
555555 // Note that we can't eliminate uncond branches if the destination has
556556 // single-entry PHI nodes. Eliminating the single-entry phi nodes would
557 // require scanning the ValueMap to update any entries that point to the phi
557 // require scanning the VMap to update any entries that point to the phi
558558 // node.
559559 BasicBlock *Dest = BI->getSuccessor(0);
560560 if (!Dest->getSinglePredecessor() || isa(Dest->begin())) {
2222 /// CloneDominatorInfo - Clone basicblock's dominator tree and, if available,
2323 /// dominance info. It is expected that basic block is already cloned.
2424 static void CloneDominatorInfo(BasicBlock *BB,
25 DenseMap &ValueMap,
25 DenseMap &VMap,
2626 DominatorTree *DT,
2727 DominanceFrontier *DF) {
2828
2929 assert (DT && "DominatorTree is not available");
30 DenseMap::iterator BI = ValueMap.find(BB);
31 assert (BI != ValueMap.end() && "BasicBlock clone is missing");
30 DenseMap::iterator BI = VMap.find(BB);
31 assert (BI != VMap.end() && "BasicBlock clone is missing");
3232 BasicBlock *NewBB = cast(BI->second);
3333
3434 // NewBB already got dominator info.
4242
4343 // NewBB's dominator is either BB's dominator or BB's dominator's clone.
4444 BasicBlock *NewBBDom = BBDom;
45 DenseMap::iterator BBDomI = ValueMap.find(BBDom);
46 if (BBDomI != ValueMap.end()) {
45 DenseMap::iterator BBDomI = VMap.find(BBDom);
46 if (BBDomI != VMap.end()) {
4747 NewBBDom = cast(BBDomI->second);
4848 if (!DT->getNode(NewBBDom))
49 CloneDominatorInfo(BBDom, ValueMap, DT, DF);
49 CloneDominatorInfo(BBDom, VMap, DT, DF);
5050 }
5151 DT->addNewBlock(NewBB, NewBBDom);
5252
5959 for (DominanceFrontier::DomSetType::iterator I = S.begin(), E = S.end();
6060 I != E; ++I) {
6161 BasicBlock *DB = *I;
62 DenseMap::iterator IDM = ValueMap.find(DB);
63 if (IDM != ValueMap.end())
62 DenseMap::iterator IDM = VMap.find(DB);
63 if (IDM != VMap.end())
6464 NewDFSet.insert(cast(IDM->second));
6565 else
6666 NewDFSet.insert(DB);
7070 }
7171 }
7272
73 /// CloneLoop - Clone Loop. Clone dominator info. Populate ValueMap
73 /// CloneLoop - Clone Loop. Clone dominator info. Populate VMap
7474 /// using old blocks to new blocks mapping.
7575 Loop *llvm::CloneLoop(Loop *OrigL, LPPassManager *LPM, LoopInfo *LI,
76 DenseMap &ValueMap, Pass *P) {
76 DenseMap &VMap, Pass *P) {
7777
7878 DominatorTree *DT = NULL;
7979 DominanceFrontier *DF = NULL;
103103 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
104104 I != E; ++I) {
105105 BasicBlock *BB = *I;
106 BasicBlock *NewBB = CloneBasicBlock(BB, ValueMap, ".clone");
107 ValueMap[BB] = NewBB;
106 BasicBlock *NewBB = CloneBasicBlock(BB, VMap, ".clone");
107 VMap[BB] = NewBB;
108108 if (P)
109109 LPM->cloneBasicBlockSimpleAnalysis(BB, NewBB, L);
110110 NewLoop->addBasicBlockToLoop(NewBB, LI->getBase());
116116 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
117117 I != E; ++I) {
118118 BasicBlock *BB = *I;
119 CloneDominatorInfo(BB, ValueMap, DT, DF);
119 CloneDominatorInfo(BB, VMap, DT, DF);
120120 }
121121
122122 // Process sub loops
124124 LoopNest.push_back(*I);
125125 } while (!LoopNest.empty());
126126
127 // Remap instructions to reference operands from ValueMap.
127 // Remap instructions to reference operands from VMap.
128128 for(SmallVector::iterator NBItr = NewBlocks.begin(),
129129 NBE = NewBlocks.end(); NBItr != NBE; ++NBItr) {
130130 BasicBlock *NB = *NBItr;
134134 for (unsigned index = 0, num_ops = Insn->getNumOperands();
135135 index != num_ops; ++index) {
136136 Value *Op = Insn->getOperand(index);
137 DenseMap::iterator OpItr = ValueMap.find(Op);
138 if (OpItr != ValueMap.end())
137 DenseMap::iterator OpItr = VMap.find(Op);
138 if (OpItr != VMap.end())
139139 Insn->setOperand(index, OpItr->second);
140140 }
141141 }
2727 Module *llvm::CloneModule(const Module *M) {
2828 // Create the value map that maps things from the old module over to the new
2929 // module.
30 DenseMap ValueMap;
31 return CloneModule(M, ValueMap);
30 DenseMap VMap;
31 return CloneModule(M, VMap);
3232 }
3333
3434 Module *llvm::CloneModule(const Module *M,
35 DenseMap &ValueMap) {
35 DenseMap &VMap) {
3636 // First off, we need to create the new module...
3737 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
3838 New->setDataLayout(M->getDataLayout());
5050 New->addLibrary(*I);
5151
5252 // Loop over all of the global variables, making corresponding globals in the
53 // new module. Here we add them to the ValueMap and to the new Module. We
53 // new module. Here we add them to the VMap and to the new Module. We
5454 // don't worry about attributes or initializers, they will come later.
5555 //
5656 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
6161 GlobalValue::ExternalLinkage, 0,
6262 I->getName());
6363 GV->setAlignment(I->getAlignment());
64 ValueMap[I] = GV;
64 VMap[I] = GV;
6565 }
6666
6767 // Loop over the functions in the module, making external functions as before
7070 Function::Create(cast(I->getType()->getElementType()),
7171 GlobalValue::ExternalLinkage, I->getName(), New);
7272 NF->copyAttributesFrom(I);
73 ValueMap[I] = NF;
73 VMap[I] = NF;
7474 }
7575
7676 // Loop over the aliases in the module
7777 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
7878 I != E; ++I)
79 ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
79 VMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
8080 I->getName(), NULL, New);
8181
8282 // Now that all of the things that global variable initializer can refer to
8585 //
8686 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
8787 I != E; ++I) {
88 GlobalVariable *GV = cast(ValueMap[I]);
88 GlobalVariable *GV = cast(VMap[I]);
8989 if (I->hasInitializer())
9090 GV->setInitializer(cast(MapValue(I->getInitializer(),
91 ValueMap)));
91 VMap)));
9292 GV->setLinkage(I->getLinkage());
9393 GV->setThreadLocal(I->isThreadLocal());
9494 GV->setConstant(I->isConstant());
9797 // Similarly, copy over function bodies now...
9898 //
9999 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
100 Function *F = cast(ValueMap[I]);
100 Function *F = cast(VMap[I]);
101101 if (!I->isDeclaration()) {
102102 Function::arg_iterator DestI = F->arg_begin();
103103 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
104104 ++J) {
105105 DestI->setName(J->getName());
106 ValueMap[J] = DestI++;
106 VMap[J] = DestI++;
107107 }
108108
109109 SmallVector Returns; // Ignore returns cloned.
110 CloneFunctionInto(F, I, ValueMap, Returns);
110 CloneFunctionInto(F, I, VMap, Returns);
111111 }
112112
113113 F->setLinkage(I->getLinkage());
116116 // And aliases
117117 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
118118 I != E; ++I) {
119 GlobalAlias *GA = cast(ValueMap[I]);
119 GlobalAlias *GA = cast(VMap[I]);
120120 GA->setLinkage(I->getLinkage());
121121 if (const Constant* C = I->getAliasee())
122 GA->setAliasee(cast(MapValue(C, ValueMap)));
122 GA->setAliasee(cast(MapValue(C, VMap)));
123123 }
124124
125125 // And named metadata....
128128 const NamedMDNode &NMD = *I;
129129 SmallVector MDs;
130130 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
131 MDs.push_back(cast(MapValue(NMD.getOperand(i), ValueMap)));
131 MDs.push_back(cast(MapValue(NMD.getOperand(i), VMap)));
132132 NamedMDNode::Create(New->getContext(), NMD.getName(),
133133 MDs.data(), MDs.size(), New);
134134 }
143143 BI->getAllMetadata(MDs);
144144 for (SmallVector, 4>::iterator
145145 MDI = MDs.begin(), MDE = MDs.end(); MDI != MDE; ++MDI) {
146 Value *MappedValue = MapValue(MDI->second, ValueMap);
146 Value *MappedValue = MapValue(MDI->second, VMap);
147147 if (MDI->second != MappedValue && MappedValue)
148148 BI->setMetadata(MDI->first, cast(MappedValue));
149149 }
168168 /// some edges of the callgraph may remain.
169169 static void UpdateCallGraphAfterInlining(CallSite CS,
170170 Function::iterator FirstNewBlock,
171 DenseMap &ValueMap,
171 DenseMap &VMap,
172172 InlineFunctionInfo &IFI) {
173173 CallGraph &CG = *IFI.CG;
174174 const Function *Caller = CS.getInstruction()->getParent()->getParent();
191191 for (; I != E; ++I) {
192192 const Value *OrigCall = I->first;
193193
194 DenseMap::iterator VMI = ValueMap.find(OrigCall);
194 DenseMap::iterator VMI = VMap.find(OrigCall);
195195 // Only copy the edge if the call was inlined!
196 if (VMI == ValueMap.end() || VMI->second == 0)
196 if (VMI == VMap.end() || VMI->second == 0)
197197 continue;
198198
199199 // If the call was inlined, but then constant folded, there is no edge to
284284 ClonedCodeInfo InlinedFunctionInfo;
285285 Function::iterator FirstNewBlock;
286286
287 { // Scope to destroy ValueMap after cloning.
288 DenseMap ValueMap;
287 { // Scope to destroy VMap after cloning.
288 DenseMap VMap;
289289
290290 assert(CalledFunc->arg_size() == CS.arg_size() &&
291291 "No varargs calls can be inlined!");
356356 MustClearTailCallFlags = true;
357357 }
358358
359 ValueMap[I] = ActualArg;
359 VMap[I] = ActualArg;
360360 }
361361
362362 // We want the inliner to prune the code as it copies. We would LOVE to
363363 // have no dead or constant instructions leftover after inlining occurs
364364 // (which can happen, e.g., because an argument was constant), but we'll be
365365 // happy with whatever the cloner can do.
366 CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
366 CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, Returns, ".i",
367367 &InlinedFunctionInfo, IFI.TD, TheCall);
368368
369369 // Remember the first block that is newly cloned over.
371371
372372 // Update the callgraph if requested.
373373 if (IFI.CG)
374 UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, IFI);
374 UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
375375 }
376376
377377 // If there are any alloca instructions in the block that used to be the entry
3636 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
3737
3838 /// RemapInstruction - Convert the instruction operands from referencing the
39 /// current values into those specified by ValueMap.
39 /// current values into those specified by VMap.
4040 static inline void RemapInstruction(Instruction *I,
41 DenseMap &ValueMap) {
41 DenseMap &VMap) {
4242 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
4343 Value *Op = I->getOperand(op);
44 DenseMap::iterator It = ValueMap.find(Op);
45 if (It != ValueMap.end())
44 DenseMap::iterator It = VMap.find(Op);
45 if (It != VMap.end())
4646 I->setOperand(op, It->second);
4747 }
4848 }
204204
205205 for (std::vector::iterator BB = LoopBlocks.begin(),
206206 E = LoopBlocks.end(); BB != E; ++BB) {
207 ValueToValueMapTy ValueMap;
208 BasicBlock *New = CloneBasicBlock(*BB, ValueMap, "." + Twine(It));
207 ValueToValueMapTy VMap;
208 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
209209 Header->getParent()->getBasicBlockList().push_back(New);
210210
211211 // Loop over all of the PHI nodes in the block, changing them to use the
212212 // incoming values from the previous block.
213213 if (*BB == Header)
214214 for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
215 PHINode *NewPHI = cast(ValueMap[OrigPHINode[i]]);
215 PHINode *NewPHI = cast(VMap[OrigPHINode[i]]);
216216 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
217217 if (Instruction *InValI = dyn_cast(InVal))
218218 if (It > 1 && L->contains(InValI))
219219 InVal = LastValueMap[InValI];
220 ValueMap[OrigPHINode[i]] = InVal;
220 VMap[OrigPHINode[i]] = InVal;
221221 New->getInstList().erase(NewPHI);
222222 }
223223
224224 // Update our running map of newest clones
225225 LastValueMap[*BB] = New;
226 for (ValueToValueMapTy::iterator VI = ValueMap.begin(), VE = ValueMap.end();
226 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
227227 VI != VE; ++VI)
228228 LastValueMap[VI->first] = VI->second;
229229
2727 // DenseMap. This includes any recursive calls to MapValue.
2828
2929 // Global values and non-function-local metadata do not need to be seeded into
30 // the ValueMap if they are using the identity mapping.
30 // the VM if they are using the identity mapping.
3131 if (isa(V) || isa(V) || isa(V) ||
3232 (isa(V) && !cast(V)->isFunctionLocal()))
3333 return VMSlot = const_cast(V);
124124 }
125125
126126 /// RemapInstruction - Convert the instruction operands from referencing the
127 /// current values into those specified by ValueMap.
127 /// current values into those specified by VMap.
128128 ///
129 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &ValueMap) {
129 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
130130 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
131 Value *V = MapValue(*op, ValueMap);
131 Value *V = MapValue(*op, VMap);
132132 assert(V && "Referenced value not in value map!");
133133 *op = V;
134134 }