X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FUtils%2FLinker.cpp;h=8fe9113d8215907b9f90610069e3cd9e388974ef;hb=6c2e2e52870230838380778415d9ad543e66dae3;hp=c23de0a1786d45554ebd6116067eeff8807ba612;hpb=e306d94782b1c608857738279852dcc050c66004;p=oota-llvm.git diff --git a/lib/Transforms/Utils/Linker.cpp b/lib/Transforms/Utils/Linker.cpp index c23de0a1786..8fe9113d821 100644 --- a/lib/Transforms/Utils/Linker.cpp +++ b/lib/Transforms/Utils/Linker.cpp @@ -11,15 +11,10 @@ #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 using std::cerr; using std::string; using std::map; @@ -86,7 +81,7 @@ static void PrintMap(const map &M) { // automatically handle constant references correctly as well... // static Value *RemapOperand(const Value *In, map &LocalMap, - const map *GlobalMap = 0) { + map *GlobalMap = 0) { map::const_iterator I = LocalMap.find(In); if (I != LocalMap.end()) return I->second; @@ -123,30 +118,29 @@ static Value *RemapOperand(const Value *In, map &LocalMap, Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap); Result = ConstantPointerRef::get(cast(V)); } else if (const ConstantExpr *CE = dyn_cast(CPV)) { - if (CE->getNumOperands() == 1) { - // Cast instruction, unary operator + if (CE->getOpcode() == Instruction::GetElementPtr) { + Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); + std::vector Indices; + Indices.reserve(CE->getNumOperands()-1); + for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) + Indices.push_back(cast(RemapOperand(CE->getOperand(i), + LocalMap, GlobalMap))); + + Result = ConstantExpr::getGetElementPtr(cast(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(V), - CE->getType()); + Result = ConstantExpr::getCast(cast(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(V1), - cast(V2), CE->getType()); + cast(V2)); } else { - // GetElementPtr Expression - assert(CE->getOpcode() == Instruction::GetElementPtr); - Value *Ptr = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); - std::vector Indices; - Indices.reserve(CE->getNumOperands()-1); - for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) - Indices.push_back(cast(RemapOperand(CE->getOperand(i), - LocalMap, GlobalMap))); - - Result = ConstantExpr::get(CE->getOpcode(), cast(Ptr), - Indices, CE->getType()); + assert(0 && "Unknown constant expr type!"); } } else { @@ -154,7 +148,10 @@ static Value *RemapOperand(const Value *In, map &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 &GlobalMap, + map &GlobalMap, string *Err = 0) { assert(Src && Dest && Dest->isExternal() && !Src->isExternal()); map 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... @@ -410,7 +406,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 +419,6 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) { // map 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;