1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CloneFunctionInto interface, which is used as the
11 // low-level function cloner. This is used by the CloneFunction and function
12 // inliner to do the dirty work of copying the body of a function around.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/Cloning.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Metadata.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Transforms/Utils/ValueMapper.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/Analysis/DebugInfo.h"
29 #include "llvm/ADT/SmallVector.h"
33 // CloneBasicBlock - See comments in Cloning.h
34 BasicBlock
*llvm::CloneBasicBlock(const BasicBlock
*BB
,
35 ValueToValueMapTy
&VMap
,
36 const Twine
&NameSuffix
, Function
*F
,
37 ClonedCodeInfo
*CodeInfo
) {
38 BasicBlock
*NewBB
= BasicBlock::Create(BB
->getContext(), "", F
);
39 if (BB
->hasName()) NewBB
->setName(BB
->getName()+NameSuffix
);
41 bool hasCalls
= false, hasDynamicAllocas
= false, hasStaticAllocas
= false;
43 // Loop over all instructions, and copy them over.
44 for (BasicBlock::const_iterator II
= BB
->begin(), IE
= BB
->end();
46 Instruction
*NewInst
= II
->clone();
48 NewInst
->setName(II
->getName()+NameSuffix
);
49 NewBB
->getInstList().push_back(NewInst
);
50 VMap
[II
] = NewInst
; // Add instruction map to value.
52 hasCalls
|= (isa
<CallInst
>(II
) && !isa
<DbgInfoIntrinsic
>(II
));
53 if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(II
)) {
54 if (isa
<ConstantInt
>(AI
->getArraySize()))
55 hasStaticAllocas
= true;
57 hasDynamicAllocas
= true;
62 CodeInfo
->ContainsCalls
|= hasCalls
;
63 CodeInfo
->ContainsUnwinds
|= isa
<UnwindInst
>(BB
->getTerminator());
64 CodeInfo
->ContainsDynamicAllocas
|= hasDynamicAllocas
;
65 CodeInfo
->ContainsDynamicAllocas
|= hasStaticAllocas
&&
66 BB
!= &BB
->getParent()->getEntryBlock();
71 // Clone OldFunc into NewFunc, transforming the old arguments into references to
74 void llvm::CloneFunctionInto(Function
*NewFunc
, const Function
*OldFunc
,
75 ValueToValueMapTy
&VMap
,
76 bool ModuleLevelChanges
,
77 SmallVectorImpl
<ReturnInst
*> &Returns
,
78 const char *NameSuffix
, ClonedCodeInfo
*CodeInfo
) {
79 assert(NameSuffix
&& "NameSuffix cannot be null!");
82 for (Function::const_arg_iterator I
= OldFunc
->arg_begin(),
83 E
= OldFunc
->arg_end(); I
!= E
; ++I
)
84 assert(VMap
.count(I
) && "No mapping from source argument specified!");
87 // Clone any attributes.
88 if (NewFunc
->arg_size() == OldFunc
->arg_size())
89 NewFunc
->copyAttributesFrom(OldFunc
);
91 //Some arguments were deleted with the VMap. Copy arguments one by one
92 for (Function::const_arg_iterator I
= OldFunc
->arg_begin(),
93 E
= OldFunc
->arg_end(); I
!= E
; ++I
)
94 if (Argument
* Anew
= dyn_cast
<Argument
>(VMap
[I
]))
95 Anew
->addAttr( OldFunc
->getAttributes()
96 .getParamAttributes(I
->getArgNo() + 1));
97 NewFunc
->setAttributes(NewFunc
->getAttributes()
98 .addAttr(0, OldFunc
->getAttributes()
99 .getRetAttributes()));
100 NewFunc
->setAttributes(NewFunc
->getAttributes()
101 .addAttr(~0, OldFunc
->getAttributes()
102 .getFnAttributes()));
106 // Loop over all of the basic blocks in the function, cloning them as
107 // appropriate. Note that we save BE this way in order to handle cloning of
108 // recursive functions into themselves.
110 for (Function::const_iterator BI
= OldFunc
->begin(), BE
= OldFunc
->end();
112 const BasicBlock
&BB
= *BI
;
114 // Create a new basic block and copy instructions into it!
115 BasicBlock
*CBB
= CloneBasicBlock(&BB
, VMap
, NameSuffix
, NewFunc
, CodeInfo
);
116 VMap
[&BB
] = CBB
; // Add basic block mapping.
118 if (ReturnInst
*RI
= dyn_cast
<ReturnInst
>(CBB
->getTerminator()))
119 Returns
.push_back(RI
);
122 // Loop over all of the instructions in the function, fixing up operand
123 // references as we go. This uses VMap to do all the hard work.
124 for (Function::iterator BB
= cast
<BasicBlock
>(VMap
[OldFunc
->begin()]),
125 BE
= NewFunc
->end(); BB
!= BE
; ++BB
)
126 // Loop over all instructions, fixing each one as we find it...
127 for (BasicBlock::iterator II
= BB
->begin(); II
!= BB
->end(); ++II
)
128 RemapInstruction(II
, VMap
,
129 ModuleLevelChanges
? RF_None
: RF_NoModuleLevelChanges
);
132 /// CloneFunction - Return a copy of the specified function, but without
133 /// embedding the function into another module. Also, any references specified
134 /// in the VMap are changed to refer to their mapped value instead of the
135 /// original one. If any of the arguments to the function are in the VMap,
136 /// the arguments are deleted from the resultant function. The VMap is
137 /// updated to include mappings from all of the instructions and basicblocks in
138 /// the function from their old to new values.
140 Function
*llvm::CloneFunction(const Function
*F
, ValueToValueMapTy
&VMap
,
141 bool ModuleLevelChanges
,
142 ClonedCodeInfo
*CodeInfo
) {
143 std::vector
<const Type
*> ArgTypes
;
145 // The user might be deleting arguments to the function by specifying them in
146 // the VMap. If so, we need to not add the arguments to the arg ty vector
148 for (Function::const_arg_iterator I
= F
->arg_begin(), E
= F
->arg_end();
150 if (VMap
.count(I
) == 0) // Haven't mapped the argument to anything yet?
151 ArgTypes
.push_back(I
->getType());
153 // Create a new function type...
154 FunctionType
*FTy
= FunctionType::get(F
->getFunctionType()->getReturnType(),
155 ArgTypes
, F
->getFunctionType()->isVarArg());
157 // Create the new function...
158 Function
*NewF
= Function::Create(FTy
, F
->getLinkage(), F
->getName());
160 // Loop over the arguments, copying the names of the mapped arguments over...
161 Function::arg_iterator DestI
= NewF
->arg_begin();
162 for (Function::const_arg_iterator I
= F
->arg_begin(), E
= F
->arg_end();
164 if (VMap
.count(I
) == 0) { // Is this argument preserved?
165 DestI
->setName(I
->getName()); // Copy the name over...
166 VMap
[I
] = DestI
++; // Add mapping to VMap
169 SmallVector
<ReturnInst
*, 8> Returns
; // Ignore returns cloned.
170 CloneFunctionInto(NewF
, F
, VMap
, ModuleLevelChanges
, Returns
, "", CodeInfo
);
177 /// PruningFunctionCloner - This class is a private class used to implement
178 /// the CloneAndPruneFunctionInto method.
179 struct PruningFunctionCloner
{
181 const Function
*OldFunc
;
182 ValueToValueMapTy
&VMap
;
183 bool ModuleLevelChanges
;
184 SmallVectorImpl
<ReturnInst
*> &Returns
;
185 const char *NameSuffix
;
186 ClonedCodeInfo
*CodeInfo
;
187 const TargetData
*TD
;
189 PruningFunctionCloner(Function
*newFunc
, const Function
*oldFunc
,
190 ValueToValueMapTy
&valueMap
,
191 bool moduleLevelChanges
,
192 SmallVectorImpl
<ReturnInst
*> &returns
,
193 const char *nameSuffix
,
194 ClonedCodeInfo
*codeInfo
,
195 const TargetData
*td
)
196 : NewFunc(newFunc
), OldFunc(oldFunc
),
197 VMap(valueMap
), ModuleLevelChanges(moduleLevelChanges
),
198 Returns(returns
), NameSuffix(nameSuffix
), CodeInfo(codeInfo
), TD(td
) {
201 /// CloneBlock - The specified block is found to be reachable, clone it and
202 /// anything that it can reach.
203 void CloneBlock(const BasicBlock
*BB
,
204 std::vector
<const BasicBlock
*> &ToClone
);
207 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
208 /// mapping its operands through VMap if they are available.
209 Constant
*ConstantFoldMappedInstruction(const Instruction
*I
);
213 /// CloneBlock - The specified block is found to be reachable, clone it and
214 /// anything that it can reach.
215 void PruningFunctionCloner::CloneBlock(const BasicBlock
*BB
,
216 std::vector
<const BasicBlock
*> &ToClone
){
217 TrackingVH
<Value
> &BBEntry
= VMap
[BB
];
219 // Have we already cloned this block?
222 // Nope, clone it now.
224 BBEntry
= NewBB
= BasicBlock::Create(BB
->getContext());
225 if (BB
->hasName()) NewBB
->setName(BB
->getName()+NameSuffix
);
227 bool hasCalls
= false, hasDynamicAllocas
= false, hasStaticAllocas
= false;
229 // Loop over all instructions, and copy them over, DCE'ing as we go. This
230 // loop doesn't include the terminator.
231 for (BasicBlock::const_iterator II
= BB
->begin(), IE
= --BB
->end();
233 // If this instruction constant folds, don't bother cloning the instruction,
234 // instead, just add the constant to the value map.
235 if (Constant
*C
= ConstantFoldMappedInstruction(II
)) {
240 Instruction
*NewInst
= II
->clone();
242 NewInst
->setName(II
->getName()+NameSuffix
);
243 NewBB
->getInstList().push_back(NewInst
);
244 VMap
[II
] = NewInst
; // Add instruction map to value.
246 hasCalls
|= (isa
<CallInst
>(II
) && !isa
<DbgInfoIntrinsic
>(II
));
247 if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(II
)) {
248 if (isa
<ConstantInt
>(AI
->getArraySize()))
249 hasStaticAllocas
= true;
251 hasDynamicAllocas
= true;
255 // Finally, clone over the terminator.
256 const TerminatorInst
*OldTI
= BB
->getTerminator();
257 bool TerminatorDone
= false;
258 if (const BranchInst
*BI
= dyn_cast
<BranchInst
>(OldTI
)) {
259 if (BI
->isConditional()) {
260 // If the condition was a known constant in the callee...
261 ConstantInt
*Cond
= dyn_cast
<ConstantInt
>(BI
->getCondition());
262 // Or is a known constant in the caller...
264 Value
*V
= VMap
[BI
->getCondition()];
265 Cond
= dyn_cast_or_null
<ConstantInt
>(V
);
268 // Constant fold to uncond branch!
270 BasicBlock
*Dest
= BI
->getSuccessor(!Cond
->getZExtValue());
271 VMap
[OldTI
] = BranchInst::Create(Dest
, NewBB
);
272 ToClone
.push_back(Dest
);
273 TerminatorDone
= true;
276 } else if (const SwitchInst
*SI
= dyn_cast
<SwitchInst
>(OldTI
)) {
277 // If switching on a value known constant in the caller.
278 ConstantInt
*Cond
= dyn_cast
<ConstantInt
>(SI
->getCondition());
279 if (Cond
== 0) { // Or known constant after constant prop in the callee...
280 Value
*V
= VMap
[SI
->getCondition()];
281 Cond
= dyn_cast_or_null
<ConstantInt
>(V
);
283 if (Cond
) { // Constant fold to uncond branch!
284 BasicBlock
*Dest
= SI
->getSuccessor(SI
->findCaseValue(Cond
));
285 VMap
[OldTI
] = BranchInst::Create(Dest
, NewBB
);
286 ToClone
.push_back(Dest
);
287 TerminatorDone
= true;
291 if (!TerminatorDone
) {
292 Instruction
*NewInst
= OldTI
->clone();
293 if (OldTI
->hasName())
294 NewInst
->setName(OldTI
->getName()+NameSuffix
);
295 NewBB
->getInstList().push_back(NewInst
);
296 VMap
[OldTI
] = NewInst
; // Add instruction map to value.
298 // Recursively clone any reachable successor blocks.
299 const TerminatorInst
*TI
= BB
->getTerminator();
300 for (unsigned i
= 0, e
= TI
->getNumSuccessors(); i
!= e
; ++i
)
301 ToClone
.push_back(TI
->getSuccessor(i
));
305 CodeInfo
->ContainsCalls
|= hasCalls
;
306 CodeInfo
->ContainsUnwinds
|= isa
<UnwindInst
>(OldTI
);
307 CodeInfo
->ContainsDynamicAllocas
|= hasDynamicAllocas
;
308 CodeInfo
->ContainsDynamicAllocas
|= hasStaticAllocas
&&
309 BB
!= &BB
->getParent()->front();
312 if (ReturnInst
*RI
= dyn_cast
<ReturnInst
>(NewBB
->getTerminator()))
313 Returns
.push_back(RI
);
316 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
317 /// mapping its operands through VMap if they are available.
318 Constant
*PruningFunctionCloner::
319 ConstantFoldMappedInstruction(const Instruction
*I
) {
320 SmallVector
<Constant
*, 8> Ops
;
321 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
)
322 if (Constant
*Op
= dyn_cast_or_null
<Constant
>(MapValue(I
->getOperand(i
),
324 ModuleLevelChanges
? RF_None
: RF_NoModuleLevelChanges
)))
327 return 0; // All operands not constant!
329 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(I
))
330 return ConstantFoldCompareInstOperands(CI
->getPredicate(), Ops
[0], Ops
[1],
333 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(I
))
334 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(Ops
[0]))
335 if (!LI
->isVolatile() && CE
->getOpcode() == Instruction::GetElementPtr
)
336 if (GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(CE
->getOperand(0)))
337 if (GV
->isConstant() && GV
->hasDefinitiveInitializer())
338 return ConstantFoldLoadThroughGEPConstantExpr(GV
->getInitializer(),
341 return ConstantFoldInstOperands(I
->getOpcode(), I
->getType(), &Ops
[0],
345 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
346 /// except that it does some simple constant prop and DCE on the fly. The
347 /// effect of this is to copy significantly less code in cases where (for
348 /// example) a function call with constant arguments is inlined, and those
349 /// constant arguments cause a significant amount of code in the callee to be
350 /// dead. Since this doesn't produce an exact copy of the input, it can't be
351 /// used for things like CloneFunction or CloneModule.
352 void llvm::CloneAndPruneFunctionInto(Function
*NewFunc
, const Function
*OldFunc
,
353 ValueToValueMapTy
&VMap
,
354 bool ModuleLevelChanges
,
355 SmallVectorImpl
<ReturnInst
*> &Returns
,
356 const char *NameSuffix
,
357 ClonedCodeInfo
*CodeInfo
,
358 const TargetData
*TD
,
359 Instruction
*TheCall
) {
360 assert(NameSuffix
&& "NameSuffix cannot be null!");
363 for (Function::const_arg_iterator II
= OldFunc
->arg_begin(),
364 E
= OldFunc
->arg_end(); II
!= E
; ++II
)
365 assert(VMap
.count(II
) && "No mapping from source argument specified!");
368 PruningFunctionCloner
PFC(NewFunc
, OldFunc
, VMap
, ModuleLevelChanges
,
369 Returns
, NameSuffix
, CodeInfo
, TD
);
371 // Clone the entry block, and anything recursively reachable from it.
372 std::vector
<const BasicBlock
*> CloneWorklist
;
373 CloneWorklist
.push_back(&OldFunc
->getEntryBlock());
374 while (!CloneWorklist
.empty()) {
375 const BasicBlock
*BB
= CloneWorklist
.back();
376 CloneWorklist
.pop_back();
377 PFC
.CloneBlock(BB
, CloneWorklist
);
380 // Loop over all of the basic blocks in the old function. If the block was
381 // reachable, we have cloned it and the old block is now in the value map:
382 // insert it into the new function in the right order. If not, ignore it.
384 // Defer PHI resolution until rest of function is resolved.
385 SmallVector
<const PHINode
*, 16> PHIToResolve
;
386 for (Function::const_iterator BI
= OldFunc
->begin(), BE
= OldFunc
->end();
389 BasicBlock
*NewBB
= cast_or_null
<BasicBlock
>(V
);
390 if (NewBB
== 0) continue; // Dead block.
392 // Add the new block to the new function.
393 NewFunc
->getBasicBlockList().push_back(NewBB
);
395 // Loop over all of the instructions in the block, fixing up operand
396 // references as we go. This uses VMap to do all the hard work.
398 BasicBlock::iterator I
= NewBB
->begin();
402 TheCallDL
= TheCall
->getDebugLoc();
404 // Handle PHI nodes specially, as we have to remove references to dead
406 if (PHINode
*PN
= dyn_cast
<PHINode
>(I
)) {
407 // Skip over all PHI nodes, remembering them for later.
408 BasicBlock::const_iterator OldI
= BI
->begin();
409 for (; (PN
= dyn_cast
<PHINode
>(I
)); ++I
, ++OldI
)
410 PHIToResolve
.push_back(cast
<PHINode
>(OldI
));
413 // Otherwise, remap the rest of the instructions normally.
414 for (; I
!= NewBB
->end(); ++I
)
415 RemapInstruction(I
, VMap
,
416 ModuleLevelChanges
? RF_None
: RF_NoModuleLevelChanges
);
419 // Defer PHI resolution until rest of function is resolved, PHI resolution
420 // requires the CFG to be up-to-date.
421 for (unsigned phino
= 0, e
= PHIToResolve
.size(); phino
!= e
; ) {
422 const PHINode
*OPN
= PHIToResolve
[phino
];
423 unsigned NumPreds
= OPN
->getNumIncomingValues();
424 const BasicBlock
*OldBB
= OPN
->getParent();
425 BasicBlock
*NewBB
= cast
<BasicBlock
>(VMap
[OldBB
]);
427 // Map operands for blocks that are live and remove operands for blocks
429 for (; phino
!= PHIToResolve
.size() &&
430 PHIToResolve
[phino
]->getParent() == OldBB
; ++phino
) {
431 OPN
= PHIToResolve
[phino
];
432 PHINode
*PN
= cast
<PHINode
>(VMap
[OPN
]);
433 for (unsigned pred
= 0, e
= NumPreds
; pred
!= e
; ++pred
) {
434 Value
*V
= VMap
[PN
->getIncomingBlock(pred
)];
435 if (BasicBlock
*MappedBlock
= cast_or_null
<BasicBlock
>(V
)) {
436 Value
*InVal
= MapValue(PN
->getIncomingValue(pred
),
438 ModuleLevelChanges
? RF_None
: RF_NoModuleLevelChanges
);
439 assert(InVal
&& "Unknown input value?");
440 PN
->setIncomingValue(pred
, InVal
);
441 PN
->setIncomingBlock(pred
, MappedBlock
);
443 PN
->removeIncomingValue(pred
, false);
444 --pred
, --e
; // Revisit the next entry.
449 // The loop above has removed PHI entries for those blocks that are dead
450 // and has updated others. However, if a block is live (i.e. copied over)
451 // but its terminator has been changed to not go to this block, then our
452 // phi nodes will have invalid entries. Update the PHI nodes in this
454 PHINode
*PN
= cast
<PHINode
>(NewBB
->begin());
455 NumPreds
= std::distance(pred_begin(NewBB
), pred_end(NewBB
));
456 if (NumPreds
!= PN
->getNumIncomingValues()) {
457 assert(NumPreds
< PN
->getNumIncomingValues());
458 // Count how many times each predecessor comes to this block.
459 std::map
<BasicBlock
*, unsigned> PredCount
;
460 for (pred_iterator PI
= pred_begin(NewBB
), E
= pred_end(NewBB
);
464 // Figure out how many entries to remove from each PHI.
465 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
)
466 ++PredCount
[PN
->getIncomingBlock(i
)];
468 // At this point, the excess predecessor entries are positive in the
469 // map. Loop over all of the PHIs and remove excess predecessor
471 BasicBlock::iterator I
= NewBB
->begin();
472 for (; (PN
= dyn_cast
<PHINode
>(I
)); ++I
) {
473 for (std::map
<BasicBlock
*, unsigned>::iterator PCI
=PredCount
.begin(),
474 E
= PredCount
.end(); PCI
!= E
; ++PCI
) {
475 BasicBlock
*Pred
= PCI
->first
;
476 for (unsigned NumToRemove
= PCI
->second
; NumToRemove
; --NumToRemove
)
477 PN
->removeIncomingValue(Pred
, false);
482 // If the loops above have made these phi nodes have 0 or 1 operand,
483 // replace them with undef or the input value. We must do this for
484 // correctness, because 0-operand phis are not valid.
485 PN
= cast
<PHINode
>(NewBB
->begin());
486 if (PN
->getNumIncomingValues() == 0) {
487 BasicBlock::iterator I
= NewBB
->begin();
488 BasicBlock::const_iterator OldI
= OldBB
->begin();
489 while ((PN
= dyn_cast
<PHINode
>(I
++))) {
490 Value
*NV
= UndefValue::get(PN
->getType());
491 PN
->replaceAllUsesWith(NV
);
492 assert(VMap
[OldI
] == PN
&& "VMap mismatch");
494 PN
->eraseFromParent();
498 // NOTE: We cannot eliminate single entry phi nodes here, because of
499 // VMap. Single entry phi nodes can have multiple VMap entries
500 // pointing at them. Thus, deleting one would require scanning the VMap
501 // to update any entries in it that would require that. This would be
505 // Now that the inlined function body has been fully constructed, go through
506 // and zap unconditional fall-through branches. This happen all the time when
507 // specializing code: code specialization turns conditional branches into
508 // uncond branches, and this code folds them.
509 Function::iterator I
= cast
<BasicBlock
>(VMap
[&OldFunc
->getEntryBlock()]);
510 while (I
!= NewFunc
->end()) {
511 BranchInst
*BI
= dyn_cast
<BranchInst
>(I
->getTerminator());
512 if (!BI
|| BI
->isConditional()) { ++I
; continue; }
514 // Note that we can't eliminate uncond branches if the destination has
515 // single-entry PHI nodes. Eliminating the single-entry phi nodes would
516 // require scanning the VMap to update any entries that point to the phi
518 BasicBlock
*Dest
= BI
->getSuccessor(0);
519 if (!Dest
->getSinglePredecessor() || isa
<PHINode
>(Dest
->begin())) {
523 // We know all single-entry PHI nodes in the inlined function have been
524 // removed, so we just need to splice the blocks.
525 BI
->eraseFromParent();
527 // Make all PHI nodes that referred to Dest now refer to I as their source.
528 Dest
->replaceAllUsesWith(I
);
530 // Move all the instructions in the succ to the pred.
531 I
->getInstList().splice(I
->end(), Dest
->getInstList());
533 // Remove the dest block.
534 Dest
->eraseFromParent();
536 // Do not increment I, iteratively merge all things this block branches to.