Minor changes to cloning interface
authorChris Lattner <sabre@nondot.org>
Tue, 19 Nov 2002 22:54:01 +0000 (22:54 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 19 Nov 2002 22:54:01 +0000 (22:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4770 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Transforms/Utils/Cloning.h
lib/Transforms/IPO/InlineSimple.cpp
lib/Transforms/Utils/CloneFunction.cpp

index 7434a2ece8e3eabf2d84550bff83bf4e9b7d0a38..a6cefc558985f5da4acbb43164dfcbb5260367f8 100644 (file)
@@ -12,6 +12,7 @@
 #define LLVM_TRANSFORMS_UTIlS_CLONING_H
 
 #include <vector>
+#include <map>
 class Module;
 class Function;
 class BasicBlock;
@@ -19,6 +20,28 @@ class Value;
 class CallInst;
 class ReturnInst;
 
+/// CloneModule - Return an exact copy of the specified module
+///
+Module *CloneModule(Module *M);
+
+/// 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);
+
+/// CloneFunction - Version of the function that doesn't need the ValueMap.
+///
+inline Function *CloneFunction(const Function *F) {
+  std::map<const Value*, Value*> ValueMap;
+  return CloneFunction(F, ValueMap);
+}
+
 // Clone OldFunc into NewFunc, transforming the old arguments into references to
 // ArgMap values.  Note that if NewFunc already has basic blocks, the ones
 // cloned into it will be added to the end of the function.  This function fills
@@ -26,7 +49,7 @@ class ReturnInst;
 // suffix to all values cloned.
 //
 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                       const std::vector<Value*> &ArgMap,
+                       std::map<const Value*, Value*> &ValueMap,
                        std::vector<ReturnInst*> &Returns,
                        const char *NameSuffix = "");
 
index b4542ead3e30d8b2b943ff28af90361e15edacc2..6dbab594e8cad48954196f58d4ab32d5104cc337 100644 (file)
@@ -90,9 +90,14 @@ bool InlineFunction(CallInst *CI) {
   Function::iterator LastBlock = &OrigBB->getParent()->back();
 
   // Calculate the vector of arguments to pass into the function cloner...
-  std::vector<Value*> ArgVector;
-  for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
-    ArgVector.push_back(CI->getOperand(i));
+  std::map<const Value*, Value*> ValueMap;
+  assert((unsigned)std::distance(CalledFunc->abegin(), CalledFunc->aend()) == 
+         CI->getNumOperands()-1 && "No varargs calls can be inlined yet!");
+
+  unsigned i = 1;
+  for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend();
+       I != E; ++I, ++i)
+    ValueMap[I] = CI->getOperand(i);
 
   // Since we are now done with the CallInst, we can delete it.
   delete CI;
@@ -101,7 +106,7 @@ bool InlineFunction(CallInst *CI) {
   std::vector<ReturnInst*> Returns;
 
   // Do all of the hard part of cloning the callee into the caller...
-  CloneFunctionInto(OrigBB->getParent(), CalledFunc, ArgVector, Returns, ".i");
+  CloneFunctionInto(OrigBB->getParent(), CalledFunc, ValueMap, Returns, ".i");
 
   // Loop over all of the return instructions, turning them into unconditional
   // branches to the merge point now...
index 442ff03df3d2022f06a5a2b7aef27a88e09940a4..36b3a7e70144e4bfaa756424bf703e71ee301bda 100644 (file)
@@ -37,25 +37,16 @@ static inline void RemapInstruction(Instruction *I,
 // ArgMap values.
 //
 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
-                       const std::vector<Value*> &ArgMap,
+                       std::map<const Value*, Value*> &ValueMap,
                        std::vector<ReturnInst*> &Returns,
                        const char *NameSuffix) {
   assert(NameSuffix && "NameSuffix cannot be null!");
-  assert(OldFunc->asize() == ArgMap.size() &&
-         "Improper number of argument values to map specified!");
   
-  // Keep a mapping between the original function's values and the new
-  // duplicated code's values.  This includes all of: Function arguments,
-  // instruction values, constant pool entries, and basic blocks.
-  //
-  std::map<const Value *, Value*> ValueMap;
-
-  // Add all of the function arguments to the mapping...
-  unsigned i = 0;
+#ifndef NDEBUG
   for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
-       I != E; ++I, ++i)
-    ValueMap[I] = ArgMap[i];
-
+       I != E; ++I)
+    assert(ValueMap.count(I) && "No mapping from source argument specified!");
+#endif
 
   // Loop over all of the basic blocks in the function, cloning them as
   // appropriate.  Note that we save BE this way in order to handle cloning of