• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
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 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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Transforms/Utils/Local.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/Cloning.h"
35 #include "llvm/Transforms/Utils/ValueMapper.h"
36 #include <map>
37 using namespace llvm;
38 
39 /// See comments in Cloning.h.
CloneBasicBlock(const BasicBlock * BB,ValueToValueMapTy & VMap,const Twine & NameSuffix,Function * F,ClonedCodeInfo * CodeInfo,DebugInfoFinder * DIFinder)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())
47     NewBB->setName(BB->getName() + NameSuffix);
48 
49   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
50   Module *TheModule = F ? F->getParent() : nullptr;
51 
52   // Loop over all instructions, and copy them over.
53   for (const Instruction &I : *BB) {
54     if (DIFinder && TheModule)
55       DIFinder->processInstruction(*TheModule, I);
56 
57     Instruction *NewInst = I.clone();
58     if (I.hasName())
59       NewInst->setName(I.getName() + NameSuffix);
60     NewBB->getInstList().push_back(NewInst);
61     VMap[&I] = NewInst; // Add instruction map to value.
62 
63     hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I));
64     if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
65       if (isa<ConstantInt>(AI->getArraySize()))
66         hasStaticAllocas = true;
67       else
68         hasDynamicAllocas = true;
69     }
70   }
71 
72   if (CodeInfo) {
73     CodeInfo->ContainsCalls          |= hasCalls;
74     CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
75     CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
76                                         BB != &BB->getParent()->getEntryBlock();
77   }
78   return NewBB;
79 }
80 
81 // Clone OldFunc into NewFunc, transforming the old arguments into references to
82 // VMap values.
83 //
CloneFunctionInto(Function * NewFunc,const Function * OldFunc,ValueToValueMapTy & VMap,bool ModuleLevelChanges,SmallVectorImpl<ReturnInst * > & Returns,const char * NameSuffix,ClonedCodeInfo * CodeInfo,ValueMapTypeRemapper * TypeMapper,ValueMaterializer * Materializer)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!");
92 
93 #ifndef NDEBUG
94   for (const Argument &I : OldFunc->args())
95     assert(VMap.count(&I) && "No mapping from source argument specified!");
96 #endif
97 
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);
103 
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));
110 
111   SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
112   AttributeList OldAttrs = OldFunc->getAttributes();
113 
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());
119     }
120   }
121 
122   NewFunc->setAttributes(
123       AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(),
124                          OldAttrs.getRetAttributes(), NewArgAttrs));
125 
126   bool MustCloneSP =
127       OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent();
128   DISubprogram *SP = OldFunc->getSubprogram();
129   if (SP) {
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
138     // subprogram
139     if (!MustCloneSP)
140       MD[SP].reset(SP);
141   }
142 
143   SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
144   OldFunc->getAllMetadata(MDs);
145   for (auto MD : MDs) {
146     NewFunc->addMetadata(
147         MD.first,
148         *MapMetadata(MD.second, VMap,
149                      ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
150                      TypeMapper, Materializer));
151   }
152 
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   // We also record information about dbg.value and dbg.declare to avoid
157   // duplicating the types.
158   DebugInfoFinder DIFinder;
159 
160   // Loop over all of the basic blocks in the function, cloning them as
161   // appropriate.  Note that we save BE this way in order to handle cloning of
162   // recursive functions into themselves.
163   //
164   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
165        BI != BE; ++BI) {
166     const BasicBlock &BB = *BI;
167 
168     // Create a new basic block and copy instructions into it!
169     BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo,
170                                       ModuleLevelChanges ? &DIFinder : nullptr);
171 
172     // Add basic block mapping.
173     VMap[&BB] = CBB;
174 
175     // It is only legal to clone a function if a block address within that
176     // function is never referenced outside of the function.  Given that, we
177     // want to map block addresses from the old function to block addresses in
178     // the clone. (This is different from the generic ValueMapper
179     // implementation, which generates an invalid blockaddress when
180     // cloning a function.)
181     if (BB.hasAddressTaken()) {
182       Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
183                                               const_cast<BasicBlock*>(&BB));
184       VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
185     }
186 
187     // Note return instructions for the caller.
188     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
189       Returns.push_back(RI);
190   }
191 
192   for (DISubprogram *ISP : DIFinder.subprograms())
193     if (ISP != SP)
194       VMap.MD()[ISP].reset(ISP);
195 
196   for (DICompileUnit *CU : DIFinder.compile_units())
197     VMap.MD()[CU].reset(CU);
198 
199   for (DIType *Type : DIFinder.types())
200     VMap.MD()[Type].reset(Type);
201 
202   // Loop over all of the instructions in the function, fixing up operand
203   // references as we go.  This uses VMap to do all the hard work.
204   for (Function::iterator BB =
205            cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
206                           BE = NewFunc->end();
207        BB != BE; ++BB)
208     // Loop over all instructions, fixing each one as we find it...
209     for (Instruction &II : *BB)
210       RemapInstruction(&II, VMap,
211                        ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
212                        TypeMapper, Materializer);
213 }
214 
215 /// Return a copy of the specified function and add it to that function's
216 /// module.  Also, any references specified in the VMap are changed to refer to
217 /// their mapped value instead of the original one.  If any of the arguments to
218 /// the function are in the VMap, the arguments are deleted from the resultant
219 /// function.  The VMap is updated to include mappings from all of the
220 /// instructions and basicblocks in the function from their old to new values.
221 ///
CloneFunction(Function * F,ValueToValueMapTy & VMap,ClonedCodeInfo * CodeInfo)222 Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
223                               ClonedCodeInfo *CodeInfo) {
224   std::vector<Type*> ArgTypes;
225 
226   // The user might be deleting arguments to the function by specifying them in
227   // the VMap.  If so, we need to not add the arguments to the arg ty vector
228   //
229   for (const Argument &I : F->args())
230     if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
231       ArgTypes.push_back(I.getType());
232 
233   // Create a new function type...
234   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
235                                     ArgTypes, F->getFunctionType()->isVarArg());
236 
237   // Create the new function...
238   Function *NewF =
239       Function::Create(FTy, F->getLinkage(), F->getName(), F->getParent());
240 
241   // Loop over the arguments, copying the names of the mapped arguments over...
242   Function::arg_iterator DestI = NewF->arg_begin();
243   for (const Argument & I : F->args())
244     if (VMap.count(&I) == 0) {     // Is this argument preserved?
245       DestI->setName(I.getName()); // Copy the name over...
246       VMap[&I] = &*DestI++;        // Add mapping to VMap
247     }
248 
249   SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
250   CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "",
251                     CodeInfo);
252 
253   return NewF;
254 }
255 
256 
257 
258 namespace {
259   /// This is a private class used to implement CloneAndPruneFunctionInto.
260   struct PruningFunctionCloner {
261     Function *NewFunc;
262     const Function *OldFunc;
263     ValueToValueMapTy &VMap;
264     bool ModuleLevelChanges;
265     const char *NameSuffix;
266     ClonedCodeInfo *CodeInfo;
267 
268   public:
PruningFunctionCloner__anoned9122e80111::PruningFunctionCloner269     PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
270                           ValueToValueMapTy &valueMap, bool moduleLevelChanges,
271                           const char *nameSuffix, ClonedCodeInfo *codeInfo)
272         : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
273           ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
274           CodeInfo(codeInfo) {}
275 
276     /// The specified block is found to be reachable, clone it and
277     /// anything that it can reach.
278     void CloneBlock(const BasicBlock *BB,
279                     BasicBlock::const_iterator StartingInst,
280                     std::vector<const BasicBlock*> &ToClone);
281   };
282 }
283 
284 /// The specified block is found to be reachable, clone it and
285 /// anything that it can reach.
CloneBlock(const BasicBlock * BB,BasicBlock::const_iterator StartingInst,std::vector<const BasicBlock * > & ToClone)286 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
287                                        BasicBlock::const_iterator StartingInst,
288                                        std::vector<const BasicBlock*> &ToClone){
289   WeakTrackingVH &BBEntry = VMap[BB];
290 
291   // Have we already cloned this block?
292   if (BBEntry) return;
293 
294   // Nope, clone it now.
295   BasicBlock *NewBB;
296   BBEntry = NewBB = BasicBlock::Create(BB->getContext());
297   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
298 
299   // It is only legal to clone a function if a block address within that
300   // function is never referenced outside of the function.  Given that, we
301   // want to map block addresses from the old function to block addresses in
302   // the clone. (This is different from the generic ValueMapper
303   // implementation, which generates an invalid blockaddress when
304   // cloning a function.)
305   //
306   // Note that we don't need to fix the mapping for unreachable blocks;
307   // the default mapping there is safe.
308   if (BB->hasAddressTaken()) {
309     Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
310                                             const_cast<BasicBlock*>(BB));
311     VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
312   }
313 
314   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
315 
316   // Loop over all instructions, and copy them over, DCE'ing as we go.  This
317   // loop doesn't include the terminator.
318   for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
319        II != IE; ++II) {
320 
321     Instruction *NewInst = II->clone();
322 
323     // Eagerly remap operands to the newly cloned instruction, except for PHI
324     // nodes for which we defer processing until we update the CFG.
325     if (!isa<PHINode>(NewInst)) {
326       RemapInstruction(NewInst, VMap,
327                        ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
328 
329       // If we can simplify this instruction to some other value, simply add
330       // a mapping to that value rather than inserting a new instruction into
331       // the basic block.
332       if (Value *V =
333               SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
334         // On the off-chance that this simplifies to an instruction in the old
335         // function, map it back into the new function.
336         if (NewFunc != OldFunc)
337           if (Value *MappedV = VMap.lookup(V))
338             V = MappedV;
339 
340         if (!NewInst->mayHaveSideEffects()) {
341           VMap[&*II] = V;
342           NewInst->deleteValue();
343           continue;
344         }
345       }
346     }
347 
348     if (II->hasName())
349       NewInst->setName(II->getName()+NameSuffix);
350     VMap[&*II] = NewInst; // Add instruction map to value.
351     NewBB->getInstList().push_back(NewInst);
352     hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
353 
354     if (CodeInfo)
355       if (auto CS = ImmutableCallSite(&*II))
356         if (CS.hasOperandBundles())
357           CodeInfo->OperandBundleCallSites.push_back(NewInst);
358 
359     if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
360       if (isa<ConstantInt>(AI->getArraySize()))
361         hasStaticAllocas = true;
362       else
363         hasDynamicAllocas = true;
364     }
365   }
366 
367   // Finally, clone over the terminator.
368   const TerminatorInst *OldTI = BB->getTerminator();
369   bool TerminatorDone = false;
370   if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
371     if (BI->isConditional()) {
372       // If the condition was a known constant in the callee...
373       ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
374       // Or is a known constant in the caller...
375       if (!Cond) {
376         Value *V = VMap.lookup(BI->getCondition());
377         Cond = dyn_cast_or_null<ConstantInt>(V);
378       }
379 
380       // Constant fold to uncond branch!
381       if (Cond) {
382         BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
383         VMap[OldTI] = BranchInst::Create(Dest, NewBB);
384         ToClone.push_back(Dest);
385         TerminatorDone = true;
386       }
387     }
388   } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
389     // If switching on a value known constant in the caller.
390     ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
391     if (!Cond) { // Or known constant after constant prop in the callee...
392       Value *V = VMap.lookup(SI->getCondition());
393       Cond = dyn_cast_or_null<ConstantInt>(V);
394     }
395     if (Cond) {     // Constant fold to uncond branch!
396       SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
397       BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
398       VMap[OldTI] = BranchInst::Create(Dest, NewBB);
399       ToClone.push_back(Dest);
400       TerminatorDone = true;
401     }
402   }
403 
404   if (!TerminatorDone) {
405     Instruction *NewInst = OldTI->clone();
406     if (OldTI->hasName())
407       NewInst->setName(OldTI->getName()+NameSuffix);
408     NewBB->getInstList().push_back(NewInst);
409     VMap[OldTI] = NewInst;             // Add instruction map to value.
410 
411     if (CodeInfo)
412       if (auto CS = ImmutableCallSite(OldTI))
413         if (CS.hasOperandBundles())
414           CodeInfo->OperandBundleCallSites.push_back(NewInst);
415 
416     // Recursively clone any reachable successor blocks.
417     const TerminatorInst *TI = BB->getTerminator();
418     for (const BasicBlock *Succ : TI->successors())
419       ToClone.push_back(Succ);
420   }
421 
422   if (CodeInfo) {
423     CodeInfo->ContainsCalls          |= hasCalls;
424     CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
425     CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
426       BB != &BB->getParent()->front();
427   }
428 }
429 
430 /// This works like CloneAndPruneFunctionInto, except that it does not clone the
431 /// entire function. Instead it starts at an instruction provided by the caller
432 /// and copies (and prunes) only the code reachable from that instruction.
CloneAndPruneIntoFromInst(Function * NewFunc,const Function * OldFunc,const Instruction * StartingInst,ValueToValueMapTy & VMap,bool ModuleLevelChanges,SmallVectorImpl<ReturnInst * > & Returns,const char * NameSuffix,ClonedCodeInfo * CodeInfo)433 void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
434                                      const Instruction *StartingInst,
435                                      ValueToValueMapTy &VMap,
436                                      bool ModuleLevelChanges,
437                                      SmallVectorImpl<ReturnInst *> &Returns,
438                                      const char *NameSuffix,
439                                      ClonedCodeInfo *CodeInfo) {
440   assert(NameSuffix && "NameSuffix cannot be null!");
441 
442   ValueMapTypeRemapper *TypeMapper = nullptr;
443   ValueMaterializer *Materializer = nullptr;
444 
445 #ifndef NDEBUG
446   // If the cloning starts at the beginning of the function, verify that
447   // the function arguments are mapped.
448   if (!StartingInst)
449     for (const Argument &II : OldFunc->args())
450       assert(VMap.count(&II) && "No mapping from source argument specified!");
451 #endif
452 
453   PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
454                             NameSuffix, CodeInfo);
455   const BasicBlock *StartingBB;
456   if (StartingInst)
457     StartingBB = StartingInst->getParent();
458   else {
459     StartingBB = &OldFunc->getEntryBlock();
460     StartingInst = &StartingBB->front();
461   }
462 
463   // Clone the entry block, and anything recursively reachable from it.
464   std::vector<const BasicBlock*> CloneWorklist;
465   PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
466   while (!CloneWorklist.empty()) {
467     const BasicBlock *BB = CloneWorklist.back();
468     CloneWorklist.pop_back();
469     PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
470   }
471 
472   // Loop over all of the basic blocks in the old function.  If the block was
473   // reachable, we have cloned it and the old block is now in the value map:
474   // insert it into the new function in the right order.  If not, ignore it.
475   //
476   // Defer PHI resolution until rest of function is resolved.
477   SmallVector<const PHINode*, 16> PHIToResolve;
478   for (const BasicBlock &BI : *OldFunc) {
479     Value *V = VMap.lookup(&BI);
480     BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
481     if (!NewBB) continue;  // Dead block.
482 
483     // Add the new block to the new function.
484     NewFunc->getBasicBlockList().push_back(NewBB);
485 
486     // Handle PHI nodes specially, as we have to remove references to dead
487     // blocks.
488     for (const PHINode &PN : BI.phis()) {
489       // PHI nodes may have been remapped to non-PHI nodes by the caller or
490       // during the cloning process.
491       if (isa<PHINode>(VMap[&PN]))
492         PHIToResolve.push_back(&PN);
493       else
494         break;
495     }
496 
497     // Finally, remap the terminator instructions, as those can't be remapped
498     // until all BBs are mapped.
499     RemapInstruction(NewBB->getTerminator(), VMap,
500                      ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
501                      TypeMapper, Materializer);
502   }
503 
504   // Defer PHI resolution until rest of function is resolved, PHI resolution
505   // requires the CFG to be up-to-date.
506   for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
507     const PHINode *OPN = PHIToResolve[phino];
508     unsigned NumPreds = OPN->getNumIncomingValues();
509     const BasicBlock *OldBB = OPN->getParent();
510     BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
511 
512     // Map operands for blocks that are live and remove operands for blocks
513     // that are dead.
514     for (; phino != PHIToResolve.size() &&
515          PHIToResolve[phino]->getParent() == OldBB; ++phino) {
516       OPN = PHIToResolve[phino];
517       PHINode *PN = cast<PHINode>(VMap[OPN]);
518       for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
519         Value *V = VMap.lookup(PN->getIncomingBlock(pred));
520         if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
521           Value *InVal = MapValue(PN->getIncomingValue(pred),
522                                   VMap,
523                         ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
524           assert(InVal && "Unknown input value?");
525           PN->setIncomingValue(pred, InVal);
526           PN->setIncomingBlock(pred, MappedBlock);
527         } else {
528           PN->removeIncomingValue(pred, false);
529           --pred;  // Revisit the next entry.
530           --e;
531         }
532       }
533     }
534 
535     // The loop above has removed PHI entries for those blocks that are dead
536     // and has updated others.  However, if a block is live (i.e. copied over)
537     // but its terminator has been changed to not go to this block, then our
538     // phi nodes will have invalid entries.  Update the PHI nodes in this
539     // case.
540     PHINode *PN = cast<PHINode>(NewBB->begin());
541     NumPreds = pred_size(NewBB);
542     if (NumPreds != PN->getNumIncomingValues()) {
543       assert(NumPreds < PN->getNumIncomingValues());
544       // Count how many times each predecessor comes to this block.
545       std::map<BasicBlock*, unsigned> PredCount;
546       for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
547            PI != E; ++PI)
548         --PredCount[*PI];
549 
550       // Figure out how many entries to remove from each PHI.
551       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
552         ++PredCount[PN->getIncomingBlock(i)];
553 
554       // At this point, the excess predecessor entries are positive in the
555       // map.  Loop over all of the PHIs and remove excess predecessor
556       // entries.
557       BasicBlock::iterator I = NewBB->begin();
558       for (; (PN = dyn_cast<PHINode>(I)); ++I) {
559         for (const auto &PCI : PredCount) {
560           BasicBlock *Pred = PCI.first;
561           for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
562             PN->removeIncomingValue(Pred, false);
563         }
564       }
565     }
566 
567     // If the loops above have made these phi nodes have 0 or 1 operand,
568     // replace them with undef or the input value.  We must do this for
569     // correctness, because 0-operand phis are not valid.
570     PN = cast<PHINode>(NewBB->begin());
571     if (PN->getNumIncomingValues() == 0) {
572       BasicBlock::iterator I = NewBB->begin();
573       BasicBlock::const_iterator OldI = OldBB->begin();
574       while ((PN = dyn_cast<PHINode>(I++))) {
575         Value *NV = UndefValue::get(PN->getType());
576         PN->replaceAllUsesWith(NV);
577         assert(VMap[&*OldI] == PN && "VMap mismatch");
578         VMap[&*OldI] = NV;
579         PN->eraseFromParent();
580         ++OldI;
581       }
582     }
583   }
584 
585   // Make a second pass over the PHINodes now that all of them have been
586   // remapped into the new function, simplifying the PHINode and performing any
587   // recursive simplifications exposed. This will transparently update the
588   // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
589   // two PHINodes, the iteration over the old PHIs remains valid, and the
590   // mapping will just map us to the new node (which may not even be a PHI
591   // node).
592   const DataLayout &DL = NewFunc->getParent()->getDataLayout();
593   SmallSetVector<const Value *, 8> Worklist;
594   for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
595     if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
596       Worklist.insert(PHIToResolve[Idx]);
597 
598   // Note that we must test the size on each iteration, the worklist can grow.
599   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
600     const Value *OrigV = Worklist[Idx];
601     auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
602     if (!I)
603       continue;
604 
605     // Skip over non-intrinsic callsites, we don't want to remove any nodes from
606     // the CGSCC.
607     CallSite CS = CallSite(I);
608     if (CS && CS.getCalledFunction() && !CS.getCalledFunction()->isIntrinsic())
609       continue;
610 
611     // See if this instruction simplifies.
612     Value *SimpleV = SimplifyInstruction(I, DL);
613     if (!SimpleV)
614       continue;
615 
616     // Stash away all the uses of the old instruction so we can check them for
617     // recursive simplifications after a RAUW. This is cheaper than checking all
618     // uses of To on the recursive step in most cases.
619     for (const User *U : OrigV->users())
620       Worklist.insert(cast<Instruction>(U));
621 
622     // Replace the instruction with its simplified value.
623     I->replaceAllUsesWith(SimpleV);
624 
625     // If the original instruction had no side effects, remove it.
626     if (isInstructionTriviallyDead(I))
627       I->eraseFromParent();
628     else
629       VMap[OrigV] = I;
630   }
631 
632   // Now that the inlined function body has been fully constructed, go through
633   // and zap unconditional fall-through branches. This happens all the time when
634   // specializing code: code specialization turns conditional branches into
635   // uncond branches, and this code folds them.
636   Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
637   Function::iterator I = Begin;
638   while (I != NewFunc->end()) {
639     // We need to simplify conditional branches and switches with a constant
640     // operand. We try to prune these out when cloning, but if the
641     // simplification required looking through PHI nodes, those are only
642     // available after forming the full basic block. That may leave some here,
643     // and we still want to prune the dead code as early as possible.
644     //
645     // Do the folding before we check if the block is dead since we want code
646     // like
647     //  bb:
648     //    br i1 undef, label %bb, label %bb
649     // to be simplified to
650     //  bb:
651     //    br label %bb
652     // before we call I->getSinglePredecessor().
653     ConstantFoldTerminator(&*I);
654 
655     // Check if this block has become dead during inlining or other
656     // simplifications. Note that the first block will appear dead, as it has
657     // not yet been wired up properly.
658     if (I != Begin && (pred_begin(&*I) == pred_end(&*I) ||
659                        I->getSinglePredecessor() == &*I)) {
660       BasicBlock *DeadBB = &*I++;
661       DeleteDeadBlock(DeadBB);
662       continue;
663     }
664 
665     BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
666     if (!BI || BI->isConditional()) { ++I; continue; }
667 
668     BasicBlock *Dest = BI->getSuccessor(0);
669     if (!Dest->getSinglePredecessor()) {
670       ++I; continue;
671     }
672 
673     // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
674     // above should have zapped all of them..
675     assert(!isa<PHINode>(Dest->begin()));
676 
677     // We know all single-entry PHI nodes in the inlined function have been
678     // removed, so we just need to splice the blocks.
679     BI->eraseFromParent();
680 
681     // Make all PHI nodes that referred to Dest now refer to I as their source.
682     Dest->replaceAllUsesWith(&*I);
683 
684     // Move all the instructions in the succ to the pred.
685     I->getInstList().splice(I->end(), Dest->getInstList());
686 
687     // Remove the dest block.
688     Dest->eraseFromParent();
689 
690     // Do not increment I, iteratively merge all things this block branches to.
691   }
692 
693   // Make a final pass over the basic blocks from the old function to gather
694   // any return instructions which survived folding. We have to do this here
695   // because we can iteratively remove and merge returns above.
696   for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
697                           E = NewFunc->end();
698        I != E; ++I)
699     if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
700       Returns.push_back(RI);
701 }
702 
703 
704 /// This works exactly like CloneFunctionInto,
705 /// except that it does some simple constant prop and DCE on the fly.  The
706 /// effect of this is to copy significantly less code in cases where (for
707 /// example) a function call with constant arguments is inlined, and those
708 /// constant arguments cause a significant amount of code in the callee to be
709 /// dead.  Since this doesn't produce an exact copy of the input, it can't be
710 /// used for things like CloneFunction or CloneModule.
CloneAndPruneFunctionInto(Function * NewFunc,const Function * OldFunc,ValueToValueMapTy & VMap,bool ModuleLevelChanges,SmallVectorImpl<ReturnInst * > & Returns,const char * NameSuffix,ClonedCodeInfo * CodeInfo,Instruction * TheCall)711 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
712                                      ValueToValueMapTy &VMap,
713                                      bool ModuleLevelChanges,
714                                      SmallVectorImpl<ReturnInst*> &Returns,
715                                      const char *NameSuffix,
716                                      ClonedCodeInfo *CodeInfo,
717                                      Instruction *TheCall) {
718   CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
719                             ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
720 }
721 
722 /// Remaps instructions in \p Blocks using the mapping in \p VMap.
remapInstructionsInBlocks(const SmallVectorImpl<BasicBlock * > & Blocks,ValueToValueMapTy & VMap)723 void llvm::remapInstructionsInBlocks(
724     const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
725   // Rewrite the code to refer to itself.
726   for (auto *BB : Blocks)
727     for (auto &Inst : *BB)
728       RemapInstruction(&Inst, VMap,
729                        RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
730 }
731 
732 /// Clones a loop \p OrigLoop.  Returns the loop and the blocks in \p
733 /// Blocks.
734 ///
735 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
736 /// \p LoopDomBB.  Insert the new blocks before block specified in \p Before.
cloneLoopWithPreheader(BasicBlock * Before,BasicBlock * LoopDomBB,Loop * OrigLoop,ValueToValueMapTy & VMap,const Twine & NameSuffix,LoopInfo * LI,DominatorTree * DT,SmallVectorImpl<BasicBlock * > & Blocks)737 Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
738                                    Loop *OrigLoop, ValueToValueMapTy &VMap,
739                                    const Twine &NameSuffix, LoopInfo *LI,
740                                    DominatorTree *DT,
741                                    SmallVectorImpl<BasicBlock *> &Blocks) {
742   assert(OrigLoop->getSubLoops().empty() &&
743          "Loop to be cloned cannot have inner loop");
744   Function *F = OrigLoop->getHeader()->getParent();
745   Loop *ParentLoop = OrigLoop->getParentLoop();
746 
747   Loop *NewLoop = LI->AllocateLoop();
748   if (ParentLoop)
749     ParentLoop->addChildLoop(NewLoop);
750   else
751     LI->addTopLevelLoop(NewLoop);
752 
753   BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
754   assert(OrigPH && "No preheader");
755   BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
756   // To rename the loop PHIs.
757   VMap[OrigPH] = NewPH;
758   Blocks.push_back(NewPH);
759 
760   // Update LoopInfo.
761   if (ParentLoop)
762     ParentLoop->addBasicBlockToLoop(NewPH, *LI);
763 
764   // Update DominatorTree.
765   DT->addNewBlock(NewPH, LoopDomBB);
766 
767   for (BasicBlock *BB : OrigLoop->getBlocks()) {
768     BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
769     VMap[BB] = NewBB;
770 
771     // Update LoopInfo.
772     NewLoop->addBasicBlockToLoop(NewBB, *LI);
773 
774     // Add DominatorTree node. After seeing all blocks, update to correct IDom.
775     DT->addNewBlock(NewBB, NewPH);
776 
777     Blocks.push_back(NewBB);
778   }
779 
780   for (BasicBlock *BB : OrigLoop->getBlocks()) {
781     // Update DominatorTree.
782     BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
783     DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
784                                  cast<BasicBlock>(VMap[IDomBB]));
785   }
786 
787   // Move them physically from the end of the block list.
788   F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
789                                 NewPH);
790   F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
791                                 NewLoop->getHeader()->getIterator(), F->end());
792 
793   return NewLoop;
794 }
795 
796 /// Duplicate non-Phi instructions from the beginning of block up to
797 /// StopAt instruction into a split block between BB and its predecessor.
798 BasicBlock *
DuplicateInstructionsInSplitBetween(BasicBlock * BB,BasicBlock * PredBB,Instruction * StopAt,ValueToValueMapTy & ValueMapping,DominatorTree * DT)799 llvm::DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB,
800                                           Instruction *StopAt,
801                                           ValueToValueMapTy &ValueMapping,
802                                           DominatorTree *DT) {
803   // We are going to have to map operands from the original BB block to the new
804   // copy of the block 'NewBB'.  If there are PHI nodes in BB, evaluate them to
805   // account for entry from PredBB.
806   BasicBlock::iterator BI = BB->begin();
807   for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
808     ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
809 
810   BasicBlock *NewBB = SplitEdge(PredBB, BB, DT);
811   NewBB->setName(PredBB->getName() + ".split");
812   Instruction *NewTerm = NewBB->getTerminator();
813 
814   // Clone the non-phi instructions of BB into NewBB, keeping track of the
815   // mapping and using it to remap operands in the cloned instructions.
816   // Stop once we see the terminator too. This covers the case where BB's
817   // terminator gets replaced and StopAt == BB's terminator.
818   for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
819     Instruction *New = BI->clone();
820     New->setName(BI->getName());
821     New->insertBefore(NewTerm);
822     ValueMapping[&*BI] = New;
823 
824     // Remap operands to patch up intra-block references.
825     for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
826       if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
827         auto I = ValueMapping.find(Inst);
828         if (I != ValueMapping.end())
829           New->setOperand(i, I->second);
830       }
831   }
832 
833   return NewBB;
834 }
835