Remove support for Not ConstantExpr. This simplifies the unary case to only
[oota-llvm.git] / lib / Linker / LinkModules.cpp
index 094c46fef84b40090a1630dfe992276320c47c49..21e25b444a1d7f321bfbca1d0f637264fe611e4f 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;
@@ -105,7 +100,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
     if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
       const std::vector<Use> &Ops = CPA->getValues();
       std::vector<Constant*> Operands(Ops.size());
-      for (unsigned i = 0; i < Ops.size(); ++i)
+      for (unsigned i = 0, e = Ops.size(); i != e; ++i)
         Operands[i] = 
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
@@ -124,17 +119,17 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
       if (CE->getNumOperands() == 1) {
-        // Cast instruction, unary operator
+        // 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);
@@ -145,8 +140,7 @@ static Value *RemapOperand(const Value *In, map<const Value*, Value*> &LocalMap,
           Indices.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),
                                                         LocalMap, GlobalMap)));
 
-        Result = ConstantExpr::get(CE->getOpcode(), cast<Constant>(Ptr),
-                                   Indices, CE->getType());
+        Result = ConstantExpr::getGetElementPtr(cast<Constant>(Ptr), Indices);
       }
 
     } else {
@@ -154,7 +148,7 @@ 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, const_cast<Constant*>(CPV)));
+    LocalMap.insert(std::make_pair(In, Result));
     return Result;
   }
 
@@ -410,7 +404,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
 // the problem.  Upon failure, the Dest module could be in a modified state, and
 // shouldn't be relied on to be consistent.
 //
-bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
+bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg) {
 
   // LinkTypes - Go through the symbol table of the Src module and see if any
   // types are named in the src module that are not named in the Dst module.
@@ -423,15 +417,6 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) {
   //
   map<const Value*, Value*> ValueMap;
 
-  // FIXME:
-  // FIXME: This should be a two step process:
-  // FIXME:   1. LinkGlobals & LinkFunctionProtos
-  // FIXME:   2. LinkGlobalContents
-  // FIXME:
-  // FIXME: Global variables and functions are the same!
-  // FIXME:
-
-
   // Insert all of the globals in src into the Dest module... without
   // initializers
   if (LinkGlobals(Dest, Src, ValueMap, ErrorMsg)) return true;