Implement: Inline/cfg_preserve_test.ll
[oota-llvm.git] / lib / Transforms / Utils / CloneFunction.cpp
index 787e0d3c8b662a24dbc02333d68bad3a3b2ee9ad..0a16eb9bb2709114f6d47ae487a0bf41bb54c082 100644 (file)
@@ -10,7 +10,7 @@
 #include "llvm/iTerminators.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
-#include <map>
+#include "ValueMapper.h"
 
 // RemapInstruction - Convert the instruction operands from referencing the 
 // current values into those specified by ValueMap.
@@ -19,10 +19,7 @@ static inline void RemapInstruction(Instruction *I,
                                     std::map<const Value *, Value*> &ValueMap) {
   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
     const Value *Op = I->getOperand(op);
-    Value *V = ValueMap[Op];
-    if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
-      continue;  // Globals and constants don't get relocated
-
+    Value *V = MapValue(Op, ValueMap);
 #ifndef NDEBUG
     if (!V) {
       std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
@@ -34,6 +31,25 @@ static inline void RemapInstruction(Instruction *I,
   }
 }
 
+// CloneBasicBlock - See comments in Cloning.h
+BasicBlock *CloneBasicBlock(const BasicBlock *BB,
+                            std::map<const Value*, Value*> &ValueMap,
+                            const char *NameSuffix) {
+  BasicBlock *NewBB = new BasicBlock("");
+  if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
+
+  // Loop over all instructions copying them over...
+  for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
+       II != IE; ++II) {
+    Instruction *NewInst = II->clone();
+    if (II->hasName())
+      NewInst->setName(II->getName()+NameSuffix);
+    NewBB->getInstList().push_back(NewInst);
+    ValueMap[II] = NewInst;                // Add instruction map to value.
+  }
+  return NewBB;
+}
+
 // Clone OldFunc into NewFunc, transforming the old arguments into references to
 // ArgMap values.
 //
@@ -57,21 +73,11 @@ void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
        BI != BE; ++BI) {
     const BasicBlock &BB = *BI;
     
-    // Create a new basic block to copy instructions into!
-    BasicBlock *CBB = new BasicBlock("", NewFunc);
-    if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
+    // Create a new basic block and copy instructions into it!
+    BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
+    NewFunc->getBasicBlockList().push_back(CBB);
     ValueMap[&BB] = CBB;                       // Add basic block mapping.
 
-    // Loop over all instructions copying them over...
-    for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
-         II != IE; ++II) {
-      Instruction *NewInst = II->clone();
-      if (II->hasName())
-        NewInst->setName(II->getName()+NameSuffix);     // Name is not cloned...
-      CBB->getInstList().push_back(NewInst);
-      ValueMap[II] = NewInst;                // Add instruction map to value.
-    }
-
     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
       Returns.push_back(RI);
   }
@@ -113,14 +119,14 @@ Function *CloneFunction(const Function *F,
                                     ArgTypes, F->getFunctionType()->isVarArg());
 
   // Create the new function...
-  Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
+  Function *NewF = new Function(FTy, F->getLinkage(), 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?
+    if (ValueMap.count(I) == 0) {   // Is this argument preserved?
       DestI->setName(I->getName()); // Copy the name over...
-      ValueMap[I] = DestI;          // Add mapping to ValueMap
+      ValueMap[I] = DestI++;        // Add mapping to ValueMap
     }
 
   std::vector<ReturnInst*> Returns;  // Ignore returns cloned...