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/ADT/SetVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DebugInfo.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Metadata.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
34 #include "llvm/Transforms/Utils/Local.h"
35 #include "llvm/Transforms/Utils/ValueMapper.h"
39 /// See comments in Cloning.h.
40 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
41 const Twine &NameSuffix, Function *F,
42 ClonedCodeInfo *CodeInfo,
43 DebugInfoFinder *DIFinder) {
44 DenseMap<const MDNode *, MDNode *> Cache;
45 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
46 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
48 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
50 // Loop over all instructions, and copy them over.
51 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
54 if (DIFinder && F->getParent() && II->getDebugLoc())
55 DIFinder->processLocation(*F->getParent(), II->getDebugLoc().get());
57 Instruction *NewInst = II->clone();
59 NewInst->setName(II->getName()+NameSuffix);
60 NewBB->getInstList().push_back(NewInst);
61 VMap[&*II] = NewInst; // Add instruction map to value.
63 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
64 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
65 if (isa<ConstantInt>(AI->getArraySize()))
66 hasStaticAllocas = true;
68 hasDynamicAllocas = true;
73 CodeInfo->ContainsCalls |= hasCalls;
74 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
75 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
76 BB != &BB->getParent()->getEntryBlock();
81 // Clone OldFunc into NewFunc, transforming the old arguments into references to
84 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
85 ValueToValueMapTy &VMap,
86 bool ModuleLevelChanges,
87 SmallVectorImpl<ReturnInst*> &Returns,
88 const char *NameSuffix, ClonedCodeInfo *CodeInfo,
89 ValueMapTypeRemapper *TypeMapper,
90 ValueMaterializer *Materializer) {
91 assert(NameSuffix && "NameSuffix cannot be null!");
94 for (const Argument &I : OldFunc->args())
95 assert(VMap.count(&I) && "No mapping from source argument specified!");
98 // Copy all attributes other than those stored in the AttributeList. We need
99 // to remap the parameter indices of the AttributeList.
100 AttributeList NewAttrs = NewFunc->getAttributes();
101 NewFunc->copyAttributesFrom(OldFunc);
102 NewFunc->setAttributes(NewAttrs);
104 // Fix up the personality function that got copied over.
105 if (OldFunc->hasPersonalityFn())
106 NewFunc->setPersonalityFn(
107 MapValue(OldFunc->getPersonalityFn(), VMap,
108 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
109 TypeMapper, Materializer));
111 SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
112 AttributeList OldAttrs = OldFunc->getAttributes();
114 // Clone any argument attributes that are present in the VMap.
115 for (const Argument &OldArg : OldFunc->args()) {
116 if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
117 NewArgAttrs[NewArg->getArgNo()] =
118 OldAttrs.getParamAttributes(OldArg.getArgNo());
122 NewFunc->setAttributes(
123 AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(),
124 OldAttrs.getRetAttributes(), NewArgAttrs));
127 OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent();
128 DISubprogram *SP = OldFunc->getSubprogram();
130 assert(!MustCloneSP || ModuleLevelChanges);
131 // Add mappings for some DebugInfo nodes that we don't want duplicated
132 // even if they're distinct.
133 auto &MD = VMap.MD();
134 MD[SP->getUnit()].reset(SP->getUnit());
135 MD[SP->getType()].reset(SP->getType());
136 MD[SP->getFile()].reset(SP->getFile());
137 // If we're not cloning into the same module, no need to clone the
143 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
144 OldFunc->getAllMetadata(MDs);
145 for (auto MD : MDs) {
146 NewFunc->addMetadata(
148 *MapMetadata(MD.second, VMap,
149 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
150 TypeMapper, Materializer));
153 // When we remap instructions, we want to avoid duplicating inlined
154 // DISubprograms, so record all subprograms we find as we duplicate
155 // instructions and then freeze them in the MD map.
156 DebugInfoFinder DIFinder;
158 // Loop over all of the basic blocks in the function, cloning them as
159 // appropriate. Note that we save BE this way in order to handle cloning of
160 // recursive functions into themselves.
162 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
164 const BasicBlock &BB = *BI;
166 // Create a new basic block and copy instructions into it!
167 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo,
168 SP ? &DIFinder : nullptr);
170 // Add basic block mapping.
173 // It is only legal to clone a function if a block address within that
174 // function is never referenced outside of the function. Given that, we
175 // want to map block addresses from the old function to block addresses in
176 // the clone. (This is different from the generic ValueMapper
177 // implementation, which generates an invalid blockaddress when
178 // cloning a function.)
179 if (BB.hasAddressTaken()) {
180 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
181 const_cast<BasicBlock*>(&BB));
182 VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
185 // Note return instructions for the caller.
186 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
187 Returns.push_back(RI);
190 for (DISubprogram *ISP : DIFinder.subprograms()) {
192 VMap.MD()[ISP].reset(ISP);
196 // Loop over all of the instructions in the function, fixing up operand
197 // references as we go. This uses VMap to do all the hard work.
198 for (Function::iterator BB =
199 cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
202 // Loop over all instructions, fixing each one as we find it...
203 for (Instruction &II : *BB)
204 RemapInstruction(&II, VMap,
205 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
206 TypeMapper, Materializer);
209 /// Return a copy of the specified function and add it to that function's
210 /// module. Also, any references specified in the VMap are changed to refer to
211 /// their mapped value instead of the original one. If any of the arguments to
212 /// the function are in the VMap, the arguments are deleted from the resultant
213 /// function. The VMap is updated to include mappings from all of the
214 /// instructions and basicblocks in the function from their old to new values.
216 Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
217 ClonedCodeInfo *CodeInfo) {
218 std::vector<Type*> ArgTypes;
220 // The user might be deleting arguments to the function by specifying them in
221 // the VMap. If so, we need to not add the arguments to the arg ty vector
223 for (const Argument &I : F->args())
224 if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
225 ArgTypes.push_back(I.getType());
227 // Create a new function type...
228 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
229 ArgTypes, F->getFunctionType()->isVarArg());
231 // Create the new function...
233 Function::Create(FTy, F->getLinkage(), F->getName(), F->getParent());
235 // Loop over the arguments, copying the names of the mapped arguments over...
236 Function::arg_iterator DestI = NewF->arg_begin();
237 for (const Argument & I : F->args())
238 if (VMap.count(&I) == 0) { // Is this argument preserved?
239 DestI->setName(I.getName()); // Copy the name over...
240 VMap[&I] = &*DestI++; // Add mapping to VMap
243 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
244 CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "",
253 /// This is a private class used to implement CloneAndPruneFunctionInto.
254 struct PruningFunctionCloner {
256 const Function *OldFunc;
257 ValueToValueMapTy &VMap;
258 bool ModuleLevelChanges;
259 const char *NameSuffix;
260 ClonedCodeInfo *CodeInfo;
263 PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
264 ValueToValueMapTy &valueMap, bool moduleLevelChanges,
265 const char *nameSuffix, ClonedCodeInfo *codeInfo)
266 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
267 ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
268 CodeInfo(codeInfo) {}
270 /// The specified block is found to be reachable, clone it and
271 /// anything that it can reach.
272 void CloneBlock(const BasicBlock *BB,
273 BasicBlock::const_iterator StartingInst,
274 std::vector<const BasicBlock*> &ToClone);
278 /// The specified block is found to be reachable, clone it and
279 /// anything that it can reach.
280 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
281 BasicBlock::const_iterator StartingInst,
282 std::vector<const BasicBlock*> &ToClone){
283 WeakTrackingVH &BBEntry = VMap[BB];
285 // Have we already cloned this block?
288 // Nope, clone it now.
290 BBEntry = NewBB = BasicBlock::Create(BB->getContext());
291 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
293 // It is only legal to clone a function if a block address within that
294 // function is never referenced outside of the function. Given that, we
295 // want to map block addresses from the old function to block addresses in
296 // the clone. (This is different from the generic ValueMapper
297 // implementation, which generates an invalid blockaddress when
298 // cloning a function.)
300 // Note that we don't need to fix the mapping for unreachable blocks;
301 // the default mapping there is safe.
302 if (BB->hasAddressTaken()) {
303 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
304 const_cast<BasicBlock*>(BB));
305 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
308 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
310 // Loop over all instructions, and copy them over, DCE'ing as we go. This
311 // loop doesn't include the terminator.
312 for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
315 Instruction *NewInst = II->clone();
317 // Eagerly remap operands to the newly cloned instruction, except for PHI
318 // nodes for which we defer processing until we update the CFG.
319 if (!isa<PHINode>(NewInst)) {
320 RemapInstruction(NewInst, VMap,
321 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
323 // If we can simplify this instruction to some other value, simply add
324 // a mapping to that value rather than inserting a new instruction into
327 SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
328 // On the off-chance that this simplifies to an instruction in the old
329 // function, map it back into the new function.
330 if (Value *MappedV = VMap.lookup(V))
333 if (!NewInst->mayHaveSideEffects()) {
335 NewInst->deleteValue();
342 NewInst->setName(II->getName()+NameSuffix);
343 VMap[&*II] = NewInst; // Add instruction map to value.
344 NewBB->getInstList().push_back(NewInst);
345 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
348 if (auto CS = ImmutableCallSite(&*II))
349 if (CS.hasOperandBundles())
350 CodeInfo->OperandBundleCallSites.push_back(NewInst);
352 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
353 if (isa<ConstantInt>(AI->getArraySize()))
354 hasStaticAllocas = true;
356 hasDynamicAllocas = true;
360 // Finally, clone over the terminator.
361 const TerminatorInst *OldTI = BB->getTerminator();
362 bool TerminatorDone = false;
363 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
364 if (BI->isConditional()) {
365 // If the condition was a known constant in the callee...
366 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
367 // Or is a known constant in the caller...
369 Value *V = VMap.lookup(BI->getCondition());
370 Cond = dyn_cast_or_null<ConstantInt>(V);
373 // Constant fold to uncond branch!
375 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
376 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
377 ToClone.push_back(Dest);
378 TerminatorDone = true;
381 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
382 // If switching on a value known constant in the caller.
383 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
384 if (!Cond) { // Or known constant after constant prop in the callee...
385 Value *V = VMap.lookup(SI->getCondition());
386 Cond = dyn_cast_or_null<ConstantInt>(V);
388 if (Cond) { // Constant fold to uncond branch!
389 SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
390 BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
391 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
392 ToClone.push_back(Dest);
393 TerminatorDone = true;
397 if (!TerminatorDone) {
398 Instruction *NewInst = OldTI->clone();
399 if (OldTI->hasName())
400 NewInst->setName(OldTI->getName()+NameSuffix);
401 NewBB->getInstList().push_back(NewInst);
402 VMap[OldTI] = NewInst; // Add instruction map to value.
405 if (auto CS = ImmutableCallSite(OldTI))
406 if (CS.hasOperandBundles())
407 CodeInfo->OperandBundleCallSites.push_back(NewInst);
409 // Recursively clone any reachable successor blocks.
410 const TerminatorInst *TI = BB->getTerminator();
411 for (const BasicBlock *Succ : TI->successors())
412 ToClone.push_back(Succ);
416 CodeInfo->ContainsCalls |= hasCalls;
417 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
418 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
419 BB != &BB->getParent()->front();
423 /// This works like CloneAndPruneFunctionInto, except that it does not clone the
424 /// entire function. Instead it starts at an instruction provided by the caller
425 /// and copies (and prunes) only the code reachable from that instruction.
426 void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
427 const Instruction *StartingInst,
428 ValueToValueMapTy &VMap,
429 bool ModuleLevelChanges,
430 SmallVectorImpl<ReturnInst *> &Returns,
431 const char *NameSuffix,
432 ClonedCodeInfo *CodeInfo) {
433 assert(NameSuffix && "NameSuffix cannot be null!");
435 ValueMapTypeRemapper *TypeMapper = nullptr;
436 ValueMaterializer *Materializer = nullptr;
439 // If the cloning starts at the beginning of the function, verify that
440 // the function arguments are mapped.
442 for (const Argument &II : OldFunc->args())
443 assert(VMap.count(&II) && "No mapping from source argument specified!");
446 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
447 NameSuffix, CodeInfo);
448 const BasicBlock *StartingBB;
450 StartingBB = StartingInst->getParent();
452 StartingBB = &OldFunc->getEntryBlock();
453 StartingInst = &StartingBB->front();
456 // Clone the entry block, and anything recursively reachable from it.
457 std::vector<const BasicBlock*> CloneWorklist;
458 PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
459 while (!CloneWorklist.empty()) {
460 const BasicBlock *BB = CloneWorklist.back();
461 CloneWorklist.pop_back();
462 PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
465 // Loop over all of the basic blocks in the old function. If the block was
466 // reachable, we have cloned it and the old block is now in the value map:
467 // insert it into the new function in the right order. If not, ignore it.
469 // Defer PHI resolution until rest of function is resolved.
470 SmallVector<const PHINode*, 16> PHIToResolve;
471 for (const BasicBlock &BI : *OldFunc) {
472 Value *V = VMap.lookup(&BI);
473 BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
474 if (!NewBB) continue; // Dead block.
476 // Add the new block to the new function.
477 NewFunc->getBasicBlockList().push_back(NewBB);
479 // Handle PHI nodes specially, as we have to remove references to dead
481 for (BasicBlock::const_iterator I = BI.begin(), E = BI.end(); I != E; ++I) {
482 // PHI nodes may have been remapped to non-PHI nodes by the caller or
483 // during the cloning process.
484 if (const PHINode *PN = dyn_cast<PHINode>(I)) {
485 if (isa<PHINode>(VMap[PN]))
486 PHIToResolve.push_back(PN);
494 // Finally, remap the terminator instructions, as those can't be remapped
495 // until all BBs are mapped.
496 RemapInstruction(NewBB->getTerminator(), VMap,
497 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
498 TypeMapper, Materializer);
501 // Defer PHI resolution until rest of function is resolved, PHI resolution
502 // requires the CFG to be up-to-date.
503 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
504 const PHINode *OPN = PHIToResolve[phino];
505 unsigned NumPreds = OPN->getNumIncomingValues();
506 const BasicBlock *OldBB = OPN->getParent();
507 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
509 // Map operands for blocks that are live and remove operands for blocks
511 for (; phino != PHIToResolve.size() &&
512 PHIToResolve[phino]->getParent() == OldBB; ++phino) {
513 OPN = PHIToResolve[phino];
514 PHINode *PN = cast<PHINode>(VMap[OPN]);
515 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
516 Value *V = VMap.lookup(PN->getIncomingBlock(pred));
517 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
518 Value *InVal = MapValue(PN->getIncomingValue(pred),
520 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
521 assert(InVal && "Unknown input value?");
522 PN->setIncomingValue(pred, InVal);
523 PN->setIncomingBlock(pred, MappedBlock);
525 PN->removeIncomingValue(pred, false);
526 --pred; // Revisit the next entry.
532 // The loop above has removed PHI entries for those blocks that are dead
533 // and has updated others. However, if a block is live (i.e. copied over)
534 // but its terminator has been changed to not go to this block, then our
535 // phi nodes will have invalid entries. Update the PHI nodes in this
537 PHINode *PN = cast<PHINode>(NewBB->begin());
538 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
539 if (NumPreds != PN->getNumIncomingValues()) {
540 assert(NumPreds < PN->getNumIncomingValues());
541 // Count how many times each predecessor comes to this block.
542 std::map<BasicBlock*, unsigned> PredCount;
543 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
547 // Figure out how many entries to remove from each PHI.
548 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
549 ++PredCount[PN->getIncomingBlock(i)];
551 // At this point, the excess predecessor entries are positive in the
552 // map. Loop over all of the PHIs and remove excess predecessor
554 BasicBlock::iterator I = NewBB->begin();
555 for (; (PN = dyn_cast<PHINode>(I)); ++I) {
556 for (const auto &PCI : PredCount) {
557 BasicBlock *Pred = PCI.first;
558 for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
559 PN->removeIncomingValue(Pred, false);
564 // If the loops above have made these phi nodes have 0 or 1 operand,
565 // replace them with undef or the input value. We must do this for
566 // correctness, because 0-operand phis are not valid.
567 PN = cast<PHINode>(NewBB->begin());
568 if (PN->getNumIncomingValues() == 0) {
569 BasicBlock::iterator I = NewBB->begin();
570 BasicBlock::const_iterator OldI = OldBB->begin();
571 while ((PN = dyn_cast<PHINode>(I++))) {
572 Value *NV = UndefValue::get(PN->getType());
573 PN->replaceAllUsesWith(NV);
574 assert(VMap[&*OldI] == PN && "VMap mismatch");
576 PN->eraseFromParent();
582 // Make a second pass over the PHINodes now that all of them have been
583 // remapped into the new function, simplifying the PHINode and performing any
584 // recursive simplifications exposed. This will transparently update the
585 // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
586 // two PHINodes, the iteration over the old PHIs remains valid, and the
587 // mapping will just map us to the new node (which may not even be a PHI
589 const DataLayout &DL = NewFunc->getParent()->getDataLayout();
590 SmallSetVector<const Value *, 8> Worklist;
591 for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
592 if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
593 Worklist.insert(PHIToResolve[Idx]);
595 // Note that we must test the size on each iteration, the worklist can grow.
596 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
597 const Value *OrigV = Worklist[Idx];
598 auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
602 // Skip over non-intrinsic callsites, we don't want to remove any nodes from
604 CallSite CS = CallSite(I);
605 if (CS && CS.getCalledFunction() && !CS.getCalledFunction()->isIntrinsic())
608 // See if this instruction simplifies.
609 Value *SimpleV = SimplifyInstruction(I, DL);
613 // Stash away all the uses of the old instruction so we can check them for
614 // recursive simplifications after a RAUW. This is cheaper than checking all
615 // uses of To on the recursive step in most cases.
616 for (const User *U : OrigV->users())
617 Worklist.insert(cast<Instruction>(U));
619 // Replace the instruction with its simplified value.
620 I->replaceAllUsesWith(SimpleV);
622 // If the original instruction had no side effects, remove it.
623 if (isInstructionTriviallyDead(I))
624 I->eraseFromParent();
629 // Now that the inlined function body has been fully constructed, go through
630 // and zap unconditional fall-through branches. This happens all the time when
631 // specializing code: code specialization turns conditional branches into
632 // uncond branches, and this code folds them.
633 Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
634 Function::iterator I = Begin;
635 while (I != NewFunc->end()) {
636 // Check if this block has become dead during inlining or other
637 // simplifications. Note that the first block will appear dead, as it has
638 // not yet been wired up properly.
639 if (I != Begin && (pred_begin(&*I) == pred_end(&*I) ||
640 I->getSinglePredecessor() == &*I)) {
641 BasicBlock *DeadBB = &*I++;
642 DeleteDeadBlock(DeadBB);
646 // We need to simplify conditional branches and switches with a constant
647 // operand. We try to prune these out when cloning, but if the
648 // simplification required looking through PHI nodes, those are only
649 // available after forming the full basic block. That may leave some here,
650 // and we still want to prune the dead code as early as possible.
651 ConstantFoldTerminator(&*I);
653 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
654 if (!BI || BI->isConditional()) { ++I; continue; }
656 BasicBlock *Dest = BI->getSuccessor(0);
657 if (!Dest->getSinglePredecessor()) {
661 // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
662 // above should have zapped all of them..
663 assert(!isa<PHINode>(Dest->begin()));
665 // We know all single-entry PHI nodes in the inlined function have been
666 // removed, so we just need to splice the blocks.
667 BI->eraseFromParent();
669 // Make all PHI nodes that referred to Dest now refer to I as their source.
670 Dest->replaceAllUsesWith(&*I);
672 // Move all the instructions in the succ to the pred.
673 I->getInstList().splice(I->end(), Dest->getInstList());
675 // Remove the dest block.
676 Dest->eraseFromParent();
678 // Do not increment I, iteratively merge all things this block branches to.
681 // Make a final pass over the basic blocks from the old function to gather
682 // any return instructions which survived folding. We have to do this here
683 // because we can iteratively remove and merge returns above.
684 for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
687 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
688 Returns.push_back(RI);
692 /// This works exactly like CloneFunctionInto,
693 /// except that it does some simple constant prop and DCE on the fly. The
694 /// effect of this is to copy significantly less code in cases where (for
695 /// example) a function call with constant arguments is inlined, and those
696 /// constant arguments cause a significant amount of code in the callee to be
697 /// dead. Since this doesn't produce an exact copy of the input, it can't be
698 /// used for things like CloneFunction or CloneModule.
699 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
700 ValueToValueMapTy &VMap,
701 bool ModuleLevelChanges,
702 SmallVectorImpl<ReturnInst*> &Returns,
703 const char *NameSuffix,
704 ClonedCodeInfo *CodeInfo,
705 Instruction *TheCall) {
706 CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
707 ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
710 /// \brief Remaps instructions in \p Blocks using the mapping in \p VMap.
711 void llvm::remapInstructionsInBlocks(
712 const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
713 // Rewrite the code to refer to itself.
714 for (auto *BB : Blocks)
715 for (auto &Inst : *BB)
716 RemapInstruction(&Inst, VMap,
717 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
720 /// \brief Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
723 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
724 /// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
725 Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
726 Loop *OrigLoop, ValueToValueMapTy &VMap,
727 const Twine &NameSuffix, LoopInfo *LI,
729 SmallVectorImpl<BasicBlock *> &Blocks) {
730 assert(OrigLoop->getSubLoops().empty() &&
731 "Loop to be cloned cannot have inner loop");
732 Function *F = OrigLoop->getHeader()->getParent();
733 Loop *ParentLoop = OrigLoop->getParentLoop();
735 Loop *NewLoop = new Loop();
737 ParentLoop->addChildLoop(NewLoop);
739 LI->addTopLevelLoop(NewLoop);
741 BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
742 assert(OrigPH && "No preheader");
743 BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
744 // To rename the loop PHIs.
745 VMap[OrigPH] = NewPH;
746 Blocks.push_back(NewPH);
750 ParentLoop->addBasicBlockToLoop(NewPH, *LI);
752 // Update DominatorTree.
753 DT->addNewBlock(NewPH, LoopDomBB);
755 for (BasicBlock *BB : OrigLoop->getBlocks()) {
756 BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
760 NewLoop->addBasicBlockToLoop(NewBB, *LI);
762 // Add DominatorTree node. After seeing all blocks, update to correct IDom.
763 DT->addNewBlock(NewBB, NewPH);
765 Blocks.push_back(NewBB);
768 for (BasicBlock *BB : OrigLoop->getBlocks()) {
769 // Update DominatorTree.
770 BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
771 DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
772 cast<BasicBlock>(VMap[IDomBB]));
775 // Move them physically from the end of the block list.
776 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
778 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
779 NewLoop->getHeader()->getIterator(), F->end());
784 /// \brief Duplicate non-Phi instructions from the beginning of block up to
785 /// StopAt instruction into a split block between BB and its predecessor.
787 llvm::DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB,
789 ValueToValueMapTy &ValueMapping) {
790 // We are going to have to map operands from the original BB block to the new
791 // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
792 // account for entry from PredBB.
793 BasicBlock::iterator BI = BB->begin();
794 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
795 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
797 BasicBlock *NewBB = SplitEdge(PredBB, BB);
798 NewBB->setName(PredBB->getName() + ".split");
799 Instruction *NewTerm = NewBB->getTerminator();
801 // Clone the non-phi instructions of BB into NewBB, keeping track of the
802 // mapping and using it to remap operands in the cloned instructions.
803 for (; StopAt != &*BI; ++BI) {
804 Instruction *New = BI->clone();
805 New->setName(BI->getName());
806 New->insertBefore(NewTerm);
807 ValueMapping[&*BI] = New;
809 // Remap operands to patch up intra-block references.
810 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
811 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
812 auto I = ValueMapping.find(Inst);
813 if (I != ValueMapping.end())
814 New->setOperand(i, I->second);