blob: 7eaf147dc6922d6aae33ca61a449bdec55290647 [file] [log] [blame]
Chris Lattner6c2e2e52002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6c2e2e52002-11-19 22:04:49 +00009//
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 Lattnerfa703a42002-03-29 19:03:54 +000015
Chris Lattner309f1932002-11-19 20:59:41 +000016#include "llvm/Transforms/Utils/Cloning.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattner5a8932f2002-11-19 23:12:22 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerfa703a42002-03-29 19:03:54 +000019#include "llvm/Function.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020#include "ValueMapper.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner17d145d2003-04-18 03:50:09 +000023// CloneBasicBlock - See comments in Cloning.h
Chris Lattnerf7703df2004-01-09 06:12:26 +000024BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
25 std::map<const Value*, Value*> &ValueMap,
Chris Lattner23b4c682004-02-04 01:19:43 +000026 const char *NameSuffix, Function *F) {
27 BasicBlock *NewBB = new BasicBlock("", F);
Chris Lattner17d145d2003-04-18 03:50:09 +000028 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
29
30 // Loop over all instructions copying them over...
31 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
32 II != IE; ++II) {
33 Instruction *NewInst = II->clone();
34 if (II->hasName())
35 NewInst->setName(II->getName()+NameSuffix);
36 NewBB->getInstList().push_back(NewInst);
37 ValueMap[II] = NewInst; // Add instruction map to value.
38 }
39 return NewBB;
40}
41
Chris Lattnerfa703a42002-03-29 19:03:54 +000042// Clone OldFunc into NewFunc, transforming the old arguments into references to
43// ArgMap values.
44//
Chris Lattnerf7703df2004-01-09 06:12:26 +000045void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
46 std::map<const Value*, Value*> &ValueMap,
47 std::vector<ReturnInst*> &Returns,
48 const char *NameSuffix) {
Chris Lattnerdcd80402002-11-19 21:54:07 +000049 assert(NameSuffix && "NameSuffix cannot be null!");
Misha Brukmanfd939082005-04-21 23:48:37 +000050
Chris Lattnerd1801552002-11-19 22:54:01 +000051#ifndef NDEBUG
Chris Lattnere4d5c442005-03-15 04:54:21 +000052 for (Function::const_arg_iterator I = OldFunc->arg_begin(), E = OldFunc->arg_end();
Chris Lattnerd1801552002-11-19 22:54:01 +000053 I != E; ++I)
54 assert(ValueMap.count(I) && "No mapping from source argument specified!");
55#endif
Chris Lattnerfa703a42002-03-29 19:03:54 +000056
57 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerdcd80402002-11-19 21:54:07 +000058 // appropriate. Note that we save BE this way in order to handle cloning of
59 // recursive functions into themselves.
Chris Lattnerfa703a42002-03-29 19:03:54 +000060 //
61 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
62 BI != BE; ++BI) {
Chris Lattner18961502002-06-25 16:12:52 +000063 const BasicBlock &BB = *BI;
Misha Brukmanfd939082005-04-21 23:48:37 +000064
Chris Lattner17d145d2003-04-18 03:50:09 +000065 // Create a new basic block and copy instructions into it!
Chris Lattner23b4c682004-02-04 01:19:43 +000066 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc);
Chris Lattner18961502002-06-25 16:12:52 +000067 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattnerfa703a42002-03-29 19:03:54 +000068
Chris Lattnerdcd80402002-11-19 21:54:07 +000069 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
70 Returns.push_back(RI);
Chris Lattnerfa703a42002-03-29 19:03:54 +000071 }
72
Misha Brukmanfd939082005-04-21 23:48:37 +000073 // Loop over all of the instructions in the function, fixing up operand
Chris Lattnerfa703a42002-03-29 19:03:54 +000074 // references as we go. This uses ValueMap to do all the hard work.
75 //
Chris Lattnera33ceaa2004-02-04 21:44:26 +000076 for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
77 BE = NewFunc->end(); BB != BE; ++BB)
Chris Lattnerfa703a42002-03-29 19:03:54 +000078 // Loop over all instructions, fixing each one as we find it...
Chris Lattnera33ceaa2004-02-04 21:44:26 +000079 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
Chris Lattner18961502002-06-25 16:12:52 +000080 RemapInstruction(II, ValueMap);
Chris Lattnerfa703a42002-03-29 19:03:54 +000081}
Chris Lattner5a8932f2002-11-19 23:12:22 +000082
83/// CloneFunction - Return a copy of the specified function, but without
84/// embedding the function into another module. Also, any references specified
85/// in the ValueMap are changed to refer to their mapped value instead of the
86/// original one. If any of the arguments to the function are in the ValueMap,
87/// the arguments are deleted from the resultant function. The ValueMap is
88/// updated to include mappings from all of the instructions and basicblocks in
89/// the function from their old to new values.
90///
Chris Lattnerf7703df2004-01-09 06:12:26 +000091Function *llvm::CloneFunction(const Function *F,
92 std::map<const Value*, Value*> &ValueMap) {
Chris Lattner5a8932f2002-11-19 23:12:22 +000093 std::vector<const Type*> ArgTypes;
94
95 // The user might be deleting arguments to the function by specifying them in
96 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
97 //
Chris Lattnere4d5c442005-03-15 04:54:21 +000098 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner5a8932f2002-11-19 23:12:22 +000099 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
100 ArgTypes.push_back(I->getType());
101
102 // Create a new function type...
103 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
104 ArgTypes, F->getFunctionType()->isVarArg());
105
106 // Create the new function...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000107 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
Misha Brukmanfd939082005-04-21 23:48:37 +0000108
Chris Lattner5a8932f2002-11-19 23:12:22 +0000109 // Loop over the arguments, copying the names of the mapped arguments over...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000110 Function::arg_iterator DestI = NewF->arg_begin();
111 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattnerc09aab02002-11-20 18:32:31 +0000112 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattner5a8932f2002-11-19 23:12:22 +0000113 DestI->setName(I->getName()); // Copy the name over...
Chris Lattnerc09aab02002-11-20 18:32:31 +0000114 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattner5a8932f2002-11-19 23:12:22 +0000115 }
116
117 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
118 CloneFunctionInto(NewF, F, ValueMap, Returns);
Misha Brukmanfd939082005-04-21 23:48:37 +0000119 return NewF;
Chris Lattner5a8932f2002-11-19 23:12:22 +0000120}
Brian Gaeked0fde302003-11-11 22:41:34 +0000121