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/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"
30#include <map>
31using namespace llvm;
32
33// CloneBasicBlock - See comments in Cloning.h
34BasicBlock *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);
40
41  bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
42
43  // Loop over all instructions, and copy them over.
44  for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
45       II != IE; ++II) {
46    Instruction *NewInst = II->clone();
47    if (II->hasName())
48      NewInst->setName(II->getName()+NameSuffix);
49    NewBB->getInstList().push_back(NewInst);
50    VMap[II] = NewInst;                // Add instruction map to value.
51
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;
56      else
57        hasDynamicAllocas = true;
58    }
59  }
60
61  if (CodeInfo) {
62    CodeInfo->ContainsCalls          |= hasCalls;
63    CodeInfo->ContainsUnwinds        |= isa<UnwindInst>(BB->getTerminator());
64    CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
65    CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
66                                        BB != &BB->getParent()->getEntryBlock();
67  }
68  return NewBB;
69}
70
71// Clone OldFunc into NewFunc, transforming the old arguments into references to
72// VMap values.
73//
74void 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!");
80
81#ifndef NDEBUG
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!");
85#endif
86
87  // Clone any attributes.
88  if (NewFunc->arg_size() == OldFunc->arg_size())
89    NewFunc->copyAttributesFrom(OldFunc);
90  else {
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()));
103
104  }
105
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.
109  //
110  for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
111       BI != BE; ++BI) {
112    const BasicBlock &BB = *BI;
113
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.
117
118    if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
119      Returns.push_back(RI);
120  }
121
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);
130}
131
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.
139///
140Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
141                              bool ModuleLevelChanges,
142                              ClonedCodeInfo *CodeInfo) {
143  std::vector<Type*> ArgTypes;
144
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
147  //
148  for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
149       I != E; ++I)
150    if (VMap.count(I) == 0)  // Haven't mapped the argument to anything yet?
151      ArgTypes.push_back(I->getType());
152
153  // Create a new function type...
154  FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
155                                    ArgTypes, F->getFunctionType()->isVarArg());
156
157  // Create the new function...
158  Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
159
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();
163       I != E; ++I)
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
167    }
168
169  SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
170  CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
171  return NewF;
172}
173
174
175
176namespace {
177  /// PruningFunctionCloner - This class is a private class used to implement
178  /// the CloneAndPruneFunctionInto method.
179  struct PruningFunctionCloner {
180    Function *NewFunc;
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;
188  public:
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) {
199    }
200
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);
205
206  public:
207    /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
208    /// mapping its operands through VMap if they are available.
209    Constant *ConstantFoldMappedInstruction(const Instruction *I);
210  };
211}
212
213/// CloneBlock - The specified block is found to be reachable, clone it and
214/// anything that it can reach.
215void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
216                                       std::vector<const BasicBlock*> &ToClone){
217  TrackingVH<Value> &BBEntry = VMap[BB];
218
219  // Have we already cloned this block?
220  if (BBEntry) return;
221
222  // Nope, clone it now.
223  BasicBlock *NewBB;
224  BBEntry = NewBB = BasicBlock::Create(BB->getContext());
225  if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
226
227  bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
228
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();
232       II != IE; ++II) {
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)) {
236      VMap[II] = C;
237      continue;
238    }
239
240    Instruction *NewInst = II->clone();
241    if (II->hasName())
242      NewInst->setName(II->getName()+NameSuffix);
243    NewBB->getInstList().push_back(NewInst);
244    VMap[II] = NewInst;                // Add instruction map to value.
245
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;
250      else
251        hasDynamicAllocas = true;
252    }
253  }
254
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...
263      if (Cond == 0) {
264        Value *V = VMap[BI->getCondition()];
265        Cond = dyn_cast_or_null<ConstantInt>(V);
266      }
267
268      // Constant fold to uncond branch!
269      if (Cond) {
270        BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
271        VMap[OldTI] = BranchInst::Create(Dest, NewBB);
272        ToClone.push_back(Dest);
273        TerminatorDone = true;
274      }
275    }
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);
282    }
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;
288    }
289  }
290
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.
297
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));
302  }
303
304  if (CodeInfo) {
305    CodeInfo->ContainsCalls          |= hasCalls;
306    CodeInfo->ContainsUnwinds        |= isa<UnwindInst>(OldTI);
307    CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
308    CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
309      BB != &BB->getParent()->front();
310  }
311
312  if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator()))
313    Returns.push_back(RI);
314}
315
316/// ConstantFoldMappedInstruction - Constant fold the specified instruction,
317/// mapping its operands through VMap if they are available.
318Constant *PruningFunctionCloner::
319ConstantFoldMappedInstruction(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),
323                                                           VMap,
324                  ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges)))
325      Ops.push_back(Op);
326    else
327      return 0;  // All operands not constant!
328
329  if (const CmpInst *CI = dyn_cast<CmpInst>(I))
330    return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
331                                           TD);
332
333  if (const LoadInst *LI = dyn_cast<LoadInst>(I))
334    if (!LI->isVolatile())
335      return ConstantFoldLoadFromConstPtr(Ops[0], TD);
336
337  return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD);
338}
339
340/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
341/// except that it does some simple constant prop and DCE on the fly.  The
342/// effect of this is to copy significantly less code in cases where (for
343/// example) a function call with constant arguments is inlined, and those
344/// constant arguments cause a significant amount of code in the callee to be
345/// dead.  Since this doesn't produce an exact copy of the input, it can't be
346/// used for things like CloneFunction or CloneModule.
347void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
348                                     ValueToValueMapTy &VMap,
349                                     bool ModuleLevelChanges,
350                                     SmallVectorImpl<ReturnInst*> &Returns,
351                                     const char *NameSuffix,
352                                     ClonedCodeInfo *CodeInfo,
353                                     const TargetData *TD,
354                                     Instruction *TheCall) {
355  assert(NameSuffix && "NameSuffix cannot be null!");
356
357#ifndef NDEBUG
358  for (Function::const_arg_iterator II = OldFunc->arg_begin(),
359       E = OldFunc->arg_end(); II != E; ++II)
360    assert(VMap.count(II) && "No mapping from source argument specified!");
361#endif
362
363  PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
364                            Returns, NameSuffix, CodeInfo, TD);
365
366  // Clone the entry block, and anything recursively reachable from it.
367  std::vector<const BasicBlock*> CloneWorklist;
368  CloneWorklist.push_back(&OldFunc->getEntryBlock());
369  while (!CloneWorklist.empty()) {
370    const BasicBlock *BB = CloneWorklist.back();
371    CloneWorklist.pop_back();
372    PFC.CloneBlock(BB, CloneWorklist);
373  }
374
375  // Loop over all of the basic blocks in the old function.  If the block was
376  // reachable, we have cloned it and the old block is now in the value map:
377  // insert it into the new function in the right order.  If not, ignore it.
378  //
379  // Defer PHI resolution until rest of function is resolved.
380  SmallVector<const PHINode*, 16> PHIToResolve;
381  for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
382       BI != BE; ++BI) {
383    Value *V = VMap[BI];
384    BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
385    if (NewBB == 0) continue;  // Dead block.
386
387    // Add the new block to the new function.
388    NewFunc->getBasicBlockList().push_back(NewBB);
389
390    // Loop over all of the instructions in the block, fixing up operand
391    // references as we go.  This uses VMap to do all the hard work.
392    //
393    BasicBlock::iterator I = NewBB->begin();
394
395    DebugLoc TheCallDL;
396    if (TheCall)
397      TheCallDL = TheCall->getDebugLoc();
398
399    // Handle PHI nodes specially, as we have to remove references to dead
400    // blocks.
401    if (PHINode *PN = dyn_cast<PHINode>(I)) {
402      // Skip over all PHI nodes, remembering them for later.
403      BasicBlock::const_iterator OldI = BI->begin();
404      for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI)
405        PHIToResolve.push_back(cast<PHINode>(OldI));
406    }
407
408    // Otherwise, remap the rest of the instructions normally.
409    for (; I != NewBB->end(); ++I)
410      RemapInstruction(I, VMap,
411                       ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
412  }
413
414  // Defer PHI resolution until rest of function is resolved, PHI resolution
415  // requires the CFG to be up-to-date.
416  for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
417    const PHINode *OPN = PHIToResolve[phino];
418    unsigned NumPreds = OPN->getNumIncomingValues();
419    const BasicBlock *OldBB = OPN->getParent();
420    BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
421
422    // Map operands for blocks that are live and remove operands for blocks
423    // that are dead.
424    for (; phino != PHIToResolve.size() &&
425         PHIToResolve[phino]->getParent() == OldBB; ++phino) {
426      OPN = PHIToResolve[phino];
427      PHINode *PN = cast<PHINode>(VMap[OPN]);
428      for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
429        Value *V = VMap[PN->getIncomingBlock(pred)];
430        if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
431          Value *InVal = MapValue(PN->getIncomingValue(pred),
432                                  VMap,
433                        ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
434          assert(InVal && "Unknown input value?");
435          PN->setIncomingValue(pred, InVal);
436          PN->setIncomingBlock(pred, MappedBlock);
437        } else {
438          PN->removeIncomingValue(pred, false);
439          --pred, --e;  // Revisit the next entry.
440        }
441      }
442    }
443
444    // The loop above has removed PHI entries for those blocks that are dead
445    // and has updated others.  However, if a block is live (i.e. copied over)
446    // but its terminator has been changed to not go to this block, then our
447    // phi nodes will have invalid entries.  Update the PHI nodes in this
448    // case.
449    PHINode *PN = cast<PHINode>(NewBB->begin());
450    NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
451    if (NumPreds != PN->getNumIncomingValues()) {
452      assert(NumPreds < PN->getNumIncomingValues());
453      // Count how many times each predecessor comes to this block.
454      std::map<BasicBlock*, unsigned> PredCount;
455      for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
456           PI != E; ++PI)
457        --PredCount[*PI];
458
459      // Figure out how many entries to remove from each PHI.
460      for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
461        ++PredCount[PN->getIncomingBlock(i)];
462
463      // At this point, the excess predecessor entries are positive in the
464      // map.  Loop over all of the PHIs and remove excess predecessor
465      // entries.
466      BasicBlock::iterator I = NewBB->begin();
467      for (; (PN = dyn_cast<PHINode>(I)); ++I) {
468        for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
469             E = PredCount.end(); PCI != E; ++PCI) {
470          BasicBlock *Pred     = PCI->first;
471          for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
472            PN->removeIncomingValue(Pred, false);
473        }
474      }
475    }
476
477    // If the loops above have made these phi nodes have 0 or 1 operand,
478    // replace them with undef or the input value.  We must do this for
479    // correctness, because 0-operand phis are not valid.
480    PN = cast<PHINode>(NewBB->begin());
481    if (PN->getNumIncomingValues() == 0) {
482      BasicBlock::iterator I = NewBB->begin();
483      BasicBlock::const_iterator OldI = OldBB->begin();
484      while ((PN = dyn_cast<PHINode>(I++))) {
485        Value *NV = UndefValue::get(PN->getType());
486        PN->replaceAllUsesWith(NV);
487        assert(VMap[OldI] == PN && "VMap mismatch");
488        VMap[OldI] = NV;
489        PN->eraseFromParent();
490        ++OldI;
491      }
492    }
493    // NOTE: We cannot eliminate single entry phi nodes here, because of
494    // VMap.  Single entry phi nodes can have multiple VMap entries
495    // pointing at them.  Thus, deleting one would require scanning the VMap
496    // to update any entries in it that would require that.  This would be
497    // really slow.
498  }
499
500  // Now that the inlined function body has been fully constructed, go through
501  // and zap unconditional fall-through branches.  This happen all the time when
502  // specializing code: code specialization turns conditional branches into
503  // uncond branches, and this code folds them.
504  Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]);
505  while (I != NewFunc->end()) {
506    BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
507    if (!BI || BI->isConditional()) { ++I; continue; }
508
509    // Note that we can't eliminate uncond branches if the destination has
510    // single-entry PHI nodes.  Eliminating the single-entry phi nodes would
511    // require scanning the VMap to update any entries that point to the phi
512    // node.
513    BasicBlock *Dest = BI->getSuccessor(0);
514    if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) {
515      ++I; continue;
516    }
517
518    // We know all single-entry PHI nodes in the inlined function have been
519    // removed, so we just need to splice the blocks.
520    BI->eraseFromParent();
521
522    // Make all PHI nodes that referred to Dest now refer to I as their source.
523    Dest->replaceAllUsesWith(I);
524
525    // Move all the instructions in the succ to the pred.
526    I->getInstList().splice(I->end(), Dest->getInstList());
527
528    // Remove the dest block.
529    Dest->eraseFromParent();
530
531    // Do not increment I, iteratively merge all things this block branches to.
532  }
533}
534