summaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/CloneFunction.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-11-19 23:12:22 +0000
committerChris Lattner <sabre@nondot.org>2002-11-19 23:12:22 +0000
commit5a8932f57ffaf09bf5119fea142c96e5e3f653db (patch)
tree40d5302c8b054a93b0d0e6821dcb50c0d7f4ec86 /lib/Transforms/Utils/CloneFunction.cpp
parentd18015599cbe09dd327b5f73501581a865bf27da (diff)
downloadllvm-5a8932f57ffaf09bf5119fea142c96e5e3f653db.tar.gz
llvm-5a8932f57ffaf09bf5119fea142c96e5e3f653db.tar.bz2
llvm-5a8932f57ffaf09bf5119fea142c96e5e3f653db.tar.xz
Implement the CloneFunction function
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4771 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/CloneFunction.cpp')
-rw-r--r--lib/Transforms/Utils/CloneFunction.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp
index 36b3a7e701..787e0d3c8b 100644
--- a/lib/Transforms/Utils/CloneFunction.cpp
+++ b/lib/Transforms/Utils/CloneFunction.cpp
@@ -8,6 +8,7 @@
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/iTerminators.h"
+#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include <map>
@@ -87,3 +88,42 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
RemapInstruction(II, ValueMap);
}
}
+
+/// CloneFunction - Return a copy of the specified function, but without
+/// embedding the function into another module. Also, any references specified
+/// in the ValueMap are changed to refer to their mapped value instead of the
+/// original one. If any of the arguments to the function are in the ValueMap,
+/// the arguments are deleted from the resultant function. The ValueMap is
+/// updated to include mappings from all of the instructions and basicblocks in
+/// the function from their old to new values.
+///
+Function *CloneFunction(const Function *F,
+ std::map<const Value*, Value*> &ValueMap) {
+ std::vector<const Type*> ArgTypes;
+
+ // The user might be deleting arguments to the function by specifying them in
+ // the ValueMap. If so, we need to not add the arguments to the arg ty vector
+ //
+ for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+ if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
+ ArgTypes.push_back(I->getType());
+
+ // Create a new function type...
+ FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
+ ArgTypes, F->getFunctionType()->isVarArg());
+
+ // Create the new function...
+ Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
+
+ // Loop over the arguments, copying the names of the mapped arguments over...
+ Function::aiterator DestI = NewF->abegin();
+ for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+ if (ValueMap.count(I)) { // Is this argument preserved?
+ DestI->setName(I->getName()); // Copy the name over...
+ ValueMap[I] = DestI; // Add mapping to ValueMap
+ }
+
+ std::vector<ReturnInst*> Returns; // Ignore returns cloned...
+ CloneFunctionInto(NewF, F, ValueMap, Returns);
+ return NewF;
+}