@@ -845,6 +845,10 @@ static void ClearMetaData(T *value) {
845845static llvm::Type *
846846RecontextualizeType (llvm::Type *type, llvm::LLVMContext &context,
847847 TypeMap &cache) {
848+ if (&(type->getContext ()) == &context) {
849+ return type;
850+ }
851+
848852 auto &cached = cache[type];
849853 if (cached) {
850854 return cached;
@@ -883,8 +887,13 @@ RecontextualizeType(llvm::Type *type, llvm::LLVMContext &context,
883887
884888 case llvm::Type::StructTyID: {
885889 auto struct_type = llvm::dyn_cast<llvm::StructType>(type);
886- auto new_struct_type =
887- llvm::StructType::create (context, struct_type->getName ());
890+ llvm::StructType *new_struct_type = nullptr ;
891+ if (struct_type->isLiteral ()) {
892+ new_struct_type = llvm::StructType::create (context);
893+ } else {
894+ new_struct_type = llvm::StructType::create (
895+ context, struct_type->getName ());
896+ }
888897 cached = new_struct_type;
889898
890899 llvm::SmallVector<llvm::Type *, 4 > elem_types;
@@ -947,18 +956,9 @@ static llvm::Constant *MoveConstantIntoModule(llvm::Constant *c,
947956
948957 auto &dest_context = dest_module->getContext ();
949958 auto type = c->getType ();
950- auto in_same_context = true ;
951- if (&(c->getContext ()) != &dest_context) {
952- in_same_context = false ;
959+ const auto in_same_context = &(c->getContext ()) == &dest_context;
960+ if (!in_same_context) {
953961 type = RecontextualizeType (type, dest_context, type_map);
954- } else {
955- #if LLVM_VERSION_NUMBER > LLVM_VERSION(3, 8)
956- if (!llvm::isa<llvm::Function>(c) && !llvm::isa<llvm::GlobalVariable>(c) &&
957- !llvm::isa<llvm::GlobalAlias>(c) && !c->needsRelocation ()) {
958- moved_c = c;
959- return c;
960- }
961- #endif
962962 }
963963
964964 if (auto gv = llvm::dyn_cast<llvm::GlobalVariable>(c); gv) {
@@ -1305,6 +1305,14 @@ static llvm::Constant *MoveConstantIntoModule(llvm::Constant *c,
13051305 moved_c = ret;
13061306 return ret;
13071307 }
1308+ case llvm::Instruction::AddrSpaceCast: {
1309+ auto ret = llvm::ConstantExpr::getAddrSpaceCast (
1310+ MoveConstantIntoModule (ce->getOperand (0 ), dest_module, value_map,
1311+ type_map),
1312+ type);
1313+ moved_c = ret;
1314+ return ret;
1315+ }
13081316 case llvm::Instruction::GetElementPtr: {
13091317 const auto g = llvm::dyn_cast<llvm::GEPOperator>(ce);
13101318 const auto ni = g->getNumIndices ();
@@ -1544,11 +1552,10 @@ static void MoveInstructionIntoModule(llvm::Instruction *inst,
15441552 // Substitute the called function.
15451553 } else if (auto call = llvm::dyn_cast<llvm::CallInst>(inst)) {
15461554 if (auto callee_func = call->getCalledFunction ()) {
1547- if (callee_func->getParent () == dest_module) {
1548- return ;
1555+ if (callee_func->getParent () != dest_module) {
1556+ call->setCalledFunction (
1557+ DeclareFunctionInModule (callee_func, dest_module, value_map));
15491558 }
1550- call->setCalledFunction (
1551- DeclareFunctionInModule (callee_func, dest_module, value_map));
15521559
15531560 } else if (auto callee_val = call->getCalledOperand ()) {
15541561 auto &new_callee_val = value_map[callee_val];
@@ -1563,10 +1570,12 @@ static void MoveInstructionIntoModule(llvm::Instruction *inst,
15631570 }
15641571 }
15651572
1566- llvm::FunctionCallee callee (
1567- llvm::dyn_cast<llvm::FunctionType>(RecontextualizeType (
1568- call->getFunctionType (), dest_module->getContext (), type_map)),
1569- new_callee_val);
1573+ auto dest_func_type = llvm::dyn_cast<llvm::FunctionType>(
1574+ RecontextualizeType (
1575+ call->getFunctionType (), dest_module->getContext (), type_map));
1576+ CHECK_EQ (new_callee_val->getType ()->getPointerElementType (),
1577+ dest_func_type);
1578+ llvm::FunctionCallee callee (dest_func_type, new_callee_val);
15701579 call->setCalledFunction (callee);
15711580 }
15721581 }
0 commit comments