Chris Lattner | 8bce988 | 2002-11-19 22:04:49 +0000 | [diff] [blame] | 1 | //===- CloneFunction.cpp - Clone a function into another function ---------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8bce988 | 2002-11-19 22:04:49 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 1666751 | 2002-11-19 20:59:41 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 21 | #include "ValueMapper.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | e9f4232 | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 24 | // CloneBasicBlock - See comments in Cloning.h |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 25 | BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, |
| 26 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 27 | const char *NameSuffix, Function *F, |
| 28 | ClonedCodeInfo *CodeInfo) { |
Chris Lattner | a6578ef3 | 2004-02-04 01:19:43 +0000 | [diff] [blame] | 29 | BasicBlock *NewBB = new BasicBlock("", F); |
Chris Lattner | e9f4232 | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 30 | if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); |
| 31 | |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 32 | bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; |
| 33 | |
| 34 | // Loop over all instructions, and copy them over. |
Chris Lattner | e9f4232 | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 35 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); |
| 36 | II != IE; ++II) { |
| 37 | Instruction *NewInst = II->clone(); |
| 38 | if (II->hasName()) |
| 39 | NewInst->setName(II->getName()+NameSuffix); |
| 40 | NewBB->getInstList().push_back(NewInst); |
| 41 | ValueMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 42 | |
| 43 | hasCalls |= isa<CallInst>(II); |
| 44 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
| 45 | if (isa<ConstantInt>(AI->getArraySize())) |
| 46 | hasStaticAllocas = true; |
| 47 | else |
| 48 | hasDynamicAllocas = true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (CodeInfo) { |
| 53 | CodeInfo->ContainsCalls |= hasCalls; |
| 54 | CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator()); |
| 55 | CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; |
| 56 | CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && |
| 57 | BB != &BB->getParent()->front(); |
Chris Lattner | e9f4232 | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 58 | } |
| 59 | return NewBB; |
| 60 | } |
| 61 | |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 62 | // Clone OldFunc into NewFunc, transforming the old arguments into references to |
| 63 | // ArgMap values. |
| 64 | // |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 65 | void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, |
| 66 | std::map<const Value*, Value*> &ValueMap, |
| 67 | std::vector<ReturnInst*> &Returns, |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 68 | const char *NameSuffix, ClonedCodeInfo *CodeInfo) { |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 69 | assert(NameSuffix && "NameSuffix cannot be null!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 70 | |
Chris Lattner | c362618 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 71 | #ifndef NDEBUG |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 72 | for (Function::const_arg_iterator I = OldFunc->arg_begin(), |
| 73 | E = OldFunc->arg_end(); I != E; ++I) |
Chris Lattner | c362618 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 74 | assert(ValueMap.count(I) && "No mapping from source argument specified!"); |
| 75 | #endif |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 76 | |
| 77 | // Loop over all of the basic blocks in the function, cloning them as |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 78 | // appropriate. Note that we save BE this way in order to handle cloning of |
| 79 | // recursive functions into themselves. |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 80 | // |
| 81 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 82 | BI != BE; ++BI) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 83 | const BasicBlock &BB = *BI; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 84 | |
Chris Lattner | e9f4232 | 2003-04-18 03:50:09 +0000 | [diff] [blame] | 85 | // Create a new basic block and copy instructions into it! |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 86 | BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc, |
| 87 | CodeInfo); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 88 | ValueMap[&BB] = CBB; // Add basic block mapping. |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 89 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 90 | if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) |
| 91 | Returns.push_back(RI); |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 94 | // Loop over all of the instructions in the function, fixing up operand |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 95 | // references as we go. This uses ValueMap to do all the hard work. |
| 96 | // |
Chris Lattner | 39ad6f2 | 2004-02-04 21:44:26 +0000 | [diff] [blame] | 97 | for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]), |
| 98 | BE = NewFunc->end(); BB != BE; ++BB) |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 99 | // Loop over all instructions, fixing each one as we find it... |
Chris Lattner | 39ad6f2 | 2004-02-04 21:44:26 +0000 | [diff] [blame] | 100 | for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 101 | RemapInstruction(II, ValueMap); |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 103 | |
| 104 | /// CloneFunction - Return a copy of the specified function, but without |
| 105 | /// embedding the function into another module. Also, any references specified |
| 106 | /// in the ValueMap are changed to refer to their mapped value instead of the |
| 107 | /// original one. If any of the arguments to the function are in the ValueMap, |
| 108 | /// the arguments are deleted from the resultant function. The ValueMap is |
| 109 | /// updated to include mappings from all of the instructions and basicblocks in |
| 110 | /// the function from their old to new values. |
| 111 | /// |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 112 | Function *llvm::CloneFunction(const Function *F, |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 113 | std::map<const Value*, Value*> &ValueMap, |
| 114 | ClonedCodeInfo *CodeInfo) { |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 115 | std::vector<const Type*> ArgTypes; |
| 116 | |
| 117 | // The user might be deleting arguments to the function by specifying them in |
| 118 | // the ValueMap. If so, we need to not add the arguments to the arg ty vector |
| 119 | // |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 120 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 121 | I != E; ++I) |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 122 | if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet? |
| 123 | ArgTypes.push_back(I->getType()); |
| 124 | |
| 125 | // Create a new function type... |
| 126 | FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), |
| 127 | ArgTypes, F->getFunctionType()->isVarArg()); |
| 128 | |
| 129 | // Create the new function... |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 130 | Function *NewF = new Function(FTy, F->getLinkage(), F->getName()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 131 | |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 132 | // Loop over the arguments, copying the names of the mapped arguments over... |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 133 | Function::arg_iterator DestI = NewF->arg_begin(); |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 134 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 135 | I != E; ++I) |
Chris Lattner | 7c6d9d9e | 2002-11-20 18:32:31 +0000 | [diff] [blame] | 136 | if (ValueMap.count(I) == 0) { // Is this argument preserved? |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 137 | DestI->setName(I->getName()); // Copy the name over... |
Chris Lattner | 7c6d9d9e | 2002-11-20 18:32:31 +0000 | [diff] [blame] | 138 | ValueMap[I] = DestI++; // Add mapping to ValueMap |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | std::vector<ReturnInst*> Returns; // Ignore returns cloned... |
Chris Lattner | edad128 | 2006-01-13 18:39:17 +0000 | [diff] [blame] | 142 | CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 143 | return NewF; |
Chris Lattner | fb311d2 | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 144 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 145 | |