Fix two fixmes: integrate with inlining, and document
[oota-llvm.git] / lib / Transforms / Utils / Linker.cpp
index d8f67ff1aadd5857ba7555bdd88ab83b4653194e..8fe9113d8215907b9f90610069e3cd9e388974ef 100644 (file)
 
 #include "llvm/Transforms/Utils/Linker.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iOther.h"
 #include "llvm/Constants.h"
-#include "llvm/Argument.h"
-#include <iostream>
 using std::cerr;
 using std::string;
 using std::map;
@@ -86,7 +81,7 @@ static void PrintMap(const map<const Value*, Value*> &M) {
 // automatically handle constant references correctly as well...
 //
 static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
-                           const map<const Value*, Value*> *GlobalMap = 0) {
+                           map<const Value*, Value*> *GlobalMap = 0) {
   map<const Value*,Value*>::const_iterator I = LocalMap.find(In);
   if (I != LocalMap.end()) return I->second;
 
@@ -123,30 +118,29 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
-      if (CE->getNumOperands() == 1) {
-        // Cast instruction, unary operator
+      if (CE->getOpcode() == Instruction::GetElementPtr) {
+        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
+        std::vector<Constant*> Indices;
+        Indices.reserve(CE->getNumOperands()-1);
+        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
+          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
+                                                        LocalMap, GlobalMap)));
+
+        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
+      } else if (CE->getNumOperands() == 1) {
+        // Cast instruction
+        assert(CE->getOpcode() == Instruction::Cast);
         Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V),
-                                   CE->getType());
+        Result = ConstantExpr::getCast(cast<Constant>(V), CE->getType());
       } else if (CE->getNumOperands() == 2) {
         // Binary operator...
         Value *V1 = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
         Value *V2 = RemapOperand(CE->getOperand(1), LocalMap, GlobalMap);
 
         Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(V1),
-                                   cast<Constant>(V2), CE->getType());        
+                                   cast<Constant>(V2));        
       } else {
-        // GetElementPtr Expression
-        assert(CE->getOpcode() == Instruction::GetElementPtr);
-        Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap);
-        std::vector<Constant*> Indices;
-        Indices.reserve(CE->getNumOperands()-1);
-        for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
-          Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
-                                                        LocalMap, GlobalMap)));
-
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
-                                   Indices, CE->getType());
+        assert(0 && "Unknown constant expr type!");
       }
 
     } else {
@@ -154,7 +148,10 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
     }
 
     // Cache the mapping in our local map structure...
-    LocalMap.insert(std::make_pair(In, Result));
+    if (GlobalMap)
+      GlobalMap->insert(std::make_pair(In, Result));
+    else
+      LocalMap.insert(std::make_pair(In, Result));
     return Result;
   }
 
@@ -320,20 +317,19 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
 // function, and that Src is not.
 //
 static bool LinkFunctionBody(Function *Dest, const Function *Src,
-                             const map<const Value*, Value*> &GlobalMap,
+                             map<const Value*, Value*> &GlobalMap,
                              string *Err = 0) {
   assert(Src && Dest && Dest->isExternal() && !Src->isExternal());
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
+  Function::aiterator DI = Dest->abegin();
   for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
-       I != E; ++I) {
-    // Create the new function argument and add to the dest function...
-    Argument *DFA = new Argument(I->getType(), I->getName());
-    Dest->getArgumentList().push_back(DFA);
+       I != E; ++I, ++DI) {
+    DI->setName(I->getName());  // Copy the name information over...
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(I, DFA));
+    LocalMap.insert(std::make_pair(I, DI));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...