From c9ab7bf98f6a0261220e4b2380db2df8ee29829b Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 7 Jul 2009 21:07:14 +0000 Subject: [PATCH] LLVM Context-ification. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74948 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 64 +++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 4a15d88d8f3..b7ab5dff5d6 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -19,6 +19,7 @@ #include "llvm/Linker.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" +#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/TypeSymbolTable.h" #include "llvm/ValueSymbolTable.h" @@ -348,7 +349,8 @@ static void PrintMap(const std::map &M) { // RemapOperand - Use ValueMap to convert constants from one module to another. static Value *RemapOperand(const Value *In, - std::map &ValueMap) { + std::map &ValueMap, + LLVMContext &Context) { std::map::const_iterator I = ValueMap.find(In); if (I != ValueMap.end()) return I->second; @@ -363,24 +365,30 @@ static Value *RemapOperand(const Value *In, if (const ConstantArray *CPA = dyn_cast(CPV)) { std::vector Operands(CPA->getNumOperands()); for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) - Operands[i] =cast(RemapOperand(CPA->getOperand(i), ValueMap)); - Result = ConstantArray::get(cast(CPA->getType()), Operands); + Operands[i] =cast(RemapOperand(CPA->getOperand(i), ValueMap, + Context)); + Result = + Context.getConstantArray(cast(CPA->getType()), Operands); } else if (const ConstantStruct *CPS = dyn_cast(CPV)) { std::vector Operands(CPS->getNumOperands()); for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) - Operands[i] =cast(RemapOperand(CPS->getOperand(i), ValueMap)); - Result = ConstantStruct::get(cast(CPS->getType()), Operands); + Operands[i] =cast(RemapOperand(CPS->getOperand(i), ValueMap, + Context)); + Result = + Context.getConstantStruct(cast(CPS->getType()), Operands); } else if (isa(CPV) || isa(CPV)) { Result = const_cast(CPV); } else if (const ConstantVector *CP = dyn_cast(CPV)) { std::vector Operands(CP->getNumOperands()); for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) - Operands[i] = cast(RemapOperand(CP->getOperand(i), ValueMap)); - Result = ConstantVector::get(Operands); + Operands[i] = cast(RemapOperand(CP->getOperand(i), ValueMap, + Context)); + Result = Context.getConstantVector(Operands); } else if (const ConstantExpr *CE = dyn_cast(CPV)) { std::vector Ops; for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) - Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap))); + Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap, + Context))); Result = CE->getWithOperands(Ops); } else { assert(!isa(CPV) && "Unmapped global?"); @@ -528,6 +536,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, std::multimap &AppendingVars, std::string *Err) { ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable(); + LLVMContext &Context = Dest->getContext(); // Loop over all of the globals in the src module, mapping them over as we go for (Module::const_global_iterator I = Src->global_begin(), @@ -631,7 +640,8 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // Propagate alignment, section, and visibility info. CopyGVAttributes(NewDGV, SGV); - DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType())); + DGV->replaceAllUsesWith(Context.getConstantExprBitCast(NewDGV, + DGV->getType())); // DGV will conflict with NewDGV because they both had the same // name. We must erase this now so ForceRenaming doesn't assert @@ -677,7 +687,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, DGV->setLinkage(NewLinkage); // Make sure to remember this mapping... - ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType()); + ValueMap[SGV] = Context.getConstantExprBitCast(DGV, SGV->getType()); } return false; } @@ -710,6 +720,8 @@ CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) { static bool LinkAlias(Module *Dest, const Module *Src, std::map &ValueMap, std::string *Err) { + LLVMContext &Context = Dest->getContext(); + // Loop over all alias in the src module for (Module::const_alias_iterator I = Src->alias_begin(), E = Src->alias_end(); I != E; ++I) { @@ -782,7 +794,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, // Any uses of DGV need to change to NewGA, with cast, if needed. if (SGA->getType() != DGVar->getType()) - DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, + DGVar->replaceAllUsesWith(Context.getConstantExprBitCast(NewGA, DGVar->getType())); else DGVar->replaceAllUsesWith(NewGA); @@ -811,7 +823,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, // Any uses of DF need to change to NewGA, with cast, if needed. if (SGA->getType() != DF->getType()) - DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, + DF->replaceAllUsesWith(Context.getConstantExprBitCast(NewGA, DF->getType())); else DF->replaceAllUsesWith(NewGA); @@ -866,7 +878,8 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src, if (SGV->hasInitializer()) { // Only process initialized GV's // Figure out what the initializer looks like in the dest module... Constant *SInit = - cast(RemapOperand(SGV->getInitializer(), ValueMap)); + cast(RemapOperand(SGV->getInitializer(), ValueMap, + Dest->getContext())); // Grab destination global variable or alias. GlobalValue *DGV = cast(ValueMap[SGV]->stripPointerCasts()); @@ -914,6 +927,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, std::map &ValueMap, std::string *Err) { ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable(); + LLVMContext &Context = Dest->getContext(); // Loop over all of the functions in the src module, mapping them over for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { @@ -979,7 +993,8 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, CopyGVAttributes(NewDF, SF); // Any uses of DF need to change to NewDF, with cast - DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); + DGV->replaceAllUsesWith(Context.getConstantExprBitCast(NewDF, + DGV->getType())); // DF will conflict with NewDF because they both had the same. We must // erase this now so ForceRenaming doesn't assert because DF might @@ -1017,7 +1032,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, DGV->setLinkage(NewLinkage); // Make sure to remember this mapping. - ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType()); + ValueMap[SF] = Context.getConstantExprBitCast(DGV, SF->getType()); } return false; } @@ -1053,7 +1068,7 @@ static bool LinkFunctionBody(Function *Dest, Function *Src, for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); OI != OE; ++OI) if (!isa(*OI) && !isa(*OI)) - *OI = RemapOperand(*OI, ValueMap); + *OI = RemapOperand(*OI, ValueMap, *Dest->getContext()); // There is no need to map the arguments anymore. for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); @@ -1094,6 +1109,8 @@ static bool LinkAppendingVars(Module *M, std::string *ErrorMsg) { if (AppendingVars.empty()) return false; // Nothing to do. + LLVMContext &Context = M->getContext(); + // Loop over the multimap of appending vars, processing any variables with the // same name, forming a new appending global variable with both of the // initializers merged together, then rewrite references to the old variables @@ -1132,7 +1149,8 @@ static bool LinkAppendingVars(Module *M, "Appending variables with different section name need to be linked!"); unsigned NewSize = T1->getNumElements() + T2->getNumElements(); - ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); + ArrayType *NewType = Context.getArrayType(T1->getElementType(), + NewSize); G1->setName(""); // Clear G1's name in case of a conflict! @@ -1152,7 +1170,7 @@ static bool LinkAppendingVars(Module *M, Inits.push_back(I->getOperand(i)); } else { assert(isa(G1->getInitializer())); - Constant *CV = Constant::getNullValue(T1->getElementType()); + Constant *CV = Context.getNullValue(T1->getElementType()); for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) Inits.push_back(CV); } @@ -1161,11 +1179,11 @@ static bool LinkAppendingVars(Module *M, Inits.push_back(I->getOperand(i)); } else { assert(isa(G2->getInitializer())); - Constant *CV = Constant::getNullValue(T2->getElementType()); + Constant *CV = Context.getNullValue(T2->getElementType()); for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) Inits.push_back(CV); } - NG->setInitializer(ConstantArray::get(NewType, Inits)); + NG->setInitializer(Context.getConstantArray(NewType, Inits)); Inits.clear(); // Replace any uses of the two global variables with uses of the new @@ -1173,8 +1191,10 @@ static bool LinkAppendingVars(Module *M, // FIXME: This should rewrite simple/straight-forward uses such as // getelementptr instructions to not use the Cast! - G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType())); - G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType())); + G1->replaceAllUsesWith(Context.getConstantExprBitCast(NG, + G1->getType())); + G2->replaceAllUsesWith(Context.getConstantExprBitCast(NG, + G2->getType())); // Remove the two globals from the module now... M->getGlobalList().erase(G1);