From 0b0758f916cc8d4163e1bed6f67293538024586a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 18 Jul 2002 00:13:08 +0000 Subject: [PATCH] Implement linking of ConstExprs llvm-svn: 2946 --- llvm/lib/Transforms/Utils/Linker.cpp | 52 +++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Transforms/Utils/Linker.cpp b/llvm/lib/Transforms/Utils/Linker.cpp index fb83e027b06c..094c46fef84b 100644 --- a/llvm/lib/Transforms/Utils/Linker.cpp +++ b/llvm/lib/Transforms/Utils/Linker.cpp @@ -97,7 +97,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, // Check to see if it's a constant that we are interesting in transforming... if (const Constant *CPV = dyn_cast(In)) { - if (!isa(CPV->getType())) + if (!isa(CPV->getType()) && !isa(CPV)) return const_cast(CPV); // Simple constants stay identical... Constant *Result = 0; @@ -122,6 +122,33 @@ static Value *RemapOperand(const Value *In, map &LocalMap, dyn_cast(CPV)) { 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 + Value *V = RemapOperand(CE->getOperand(0), LocalMap, GlobalMap); + Result = ConstantExpr::get(CE->getOpcode(), 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()); + } 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()); + } + } else { assert(0 && "Unknown type of derived type constant value!"); } @@ -139,9 +166,7 @@ static Value *RemapOperand(const Value *In, map &LocalMap, PrintMap(*GlobalMap); } - cerr << "Couldn't remap value: " << (void*)In << " "; - In->dump(); - cerr << "\n"; + cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; assert(0 && "Couldn't remap value!"); return 0; } @@ -398,15 +423,19 @@ 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; - // Update the initializers in the Dest module now that all globals that may - // be referenced are in Dest. - // - if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; - // Link the functions together between the two modules, without doing function // bodies... this just adds external function prototypes to the Dest // function... We do this so that when we begin processing function bodies, @@ -415,6 +444,11 @@ bool LinkModules(Module *Dest, const Module *Src, string *ErrorMsg = 0) { // if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) return true; + // Update the initializers in the Dest module now that all globals that may + // be referenced are in Dest. + // + if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; + // Link in the function bodies that are defined in the source module into the // DestModule. This consists basically of copying the function over and // fixing up references to values.