diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -626,6 +626,11 @@ LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A); LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A); +/** + * Obtain a Type from a context by its registered name. + */ +LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name); + /** * @} */ @@ -867,9 +872,7 @@ */ LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M); -/** - * Obtain a Type from a module by its registered name. - */ +/** Deprecated: Use LLVMGetTypeByName2 instead. */ LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name); /** diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h --- a/llvm/include/llvm/IR/DerivedTypes.h +++ b/llvm/include/llvm/IR/DerivedTypes.h @@ -273,6 +273,10 @@ return llvm::StructType::get(Ctx, StructFields); } + /// Return the type with the specified name, or null if there is none by that + /// name. + static StructType *getTypeByName(LLVMContext &C, StringRef Name); + bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } /// Return true if this type is uniqued by structural equivalence, false if it diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -329,10 +329,6 @@ /// \see LLVMContext::getOperandBundleTagID void getOperandBundleTags(SmallVectorImpl &Result) const; - /// Return the type with the specified name, or null if there is none by that - /// name. - StructType *getTypeByName(StringRef Name) const; - std::vector getIdentifiedStructTypes() const; /// @} diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -1349,7 +1349,7 @@ VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \ VarName##Ptr = PointerType::getUnqual(VarName); #define OMP_STRUCT_TYPE(VarName, StructName, ...) \ - T = M.getTypeByName(StructName); \ + T = StructType::getTypeByName(Ctx, StructName); \ if (!T) \ T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \ VarName = T; \ diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -739,7 +739,11 @@ } LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { - return wrap(unwrap(M)->getTypeByName(Name)); + return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name)); +} + +LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) { + return wrap(StructType::getTypeByName(*unwrap(C), Name)); } /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp --- a/llvm/lib/IR/Type.cpp +++ b/llvm/lib/IR/Type.cpp @@ -533,10 +533,6 @@ return elements() == Other->elements(); } -StructType *Module::getTypeByName(StringRef Name) const { - return getContext().pImpl->NamedStructTypes.lookup(Name); -} - Type *StructType::getTypeAtIndex(const Value *V) const { unsigned Idx = (unsigned)cast(V)->getUniqueInteger().getZExtValue(); assert(indexValid(Idx) && "Invalid structure index!"); @@ -557,6 +553,10 @@ return CU && CU->getZExtValue() < getNumElements(); } +StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) { + return C.pImpl->NamedStructTypes.lookup(Name); +} + //===----------------------------------------------------------------------===// // ArrayType Implementation //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -796,11 +796,11 @@ } auto STTypePrefix = getTypeNamePrefix(ST->getName()); - if (STTypePrefix.size()== ST->getName().size()) + if (STTypePrefix.size() == ST->getName().size()) continue; // Check to see if the destination module has a struct with the prefix name. - StructType *DST = DstM.getTypeByName(STTypePrefix); + StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix); if (!DST) continue; diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -110,7 +110,7 @@ LLVMTypeRef S = nullptr; const char *Name = LLVMGetStructName(Src); if (Name) { - S = LLVMGetTypeByName(M, Name); + S = LLVMGetTypeByName2(Ctx, Name); if (S) return S; S = LLVMStructCreateNamed(Ctx, Name); diff --git a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp --- a/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp +++ b/llvm/unittests/Analysis/TargetLibraryInfoTest.cpp @@ -61,7 +61,7 @@ TEST_F(TargetLibraryInfoTest, InvalidProto) { parseAssembly("%foo = type { %foo }\n"); - auto *StructTy = M->getTypeByName("foo"); + auto *StructTy = StructType::getTypeByName(Context, "foo"); auto *InvalidFTy = FunctionType::get(StructTy, /*isVarArg=*/false); for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) { diff --git a/polly/lib/CodeGen/LoopGeneratorsKMP.cpp b/polly/lib/CodeGen/LoopGeneratorsKMP.cpp --- a/polly/lib/CodeGen/LoopGeneratorsKMP.cpp +++ b/polly/lib/CodeGen/LoopGeneratorsKMP.cpp @@ -23,7 +23,7 @@ Value *Stride) { const std::string Name = "__kmpc_fork_call"; Function *F = M->getFunction(Name); - Type *KMPCMicroTy = M->getTypeByName("kmpc_micro"); + Type *KMPCMicroTy = StructType::getTypeByName(M->getContext(), "kmpc_micro"); if (!KMPCMicroTy) { // void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...) @@ -35,7 +35,8 @@ // If F is not available, declare it. if (!F) { - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(), @@ -314,7 +315,8 @@ // If F is not available, declare it. if (!F) { - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; Type *Params[] = {IdentTy->getPointerTo()}; @@ -333,7 +335,8 @@ // If F is not available, declare it. if (!F) { - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(), @@ -356,7 +359,8 @@ const std::string Name = is64BitArch() ? "__kmpc_for_static_init_8" : "__kmpc_for_static_init_4"; Function *F = M->getFunction(Name); - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); // If F is not available, declare it. if (!F) { @@ -395,7 +399,8 @@ void ParallelLoopGeneratorKMP::createCallStaticFini(Value *GlobalThreadID) { const std::string Name = "__kmpc_for_static_fini"; Function *F = M->getFunction(Name); - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); // If F is not available, declare it. if (!F) { @@ -417,7 +422,8 @@ const std::string Name = is64BitArch() ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_4"; Function *F = M->getFunction(Name); - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); // If F is not available, declare it. if (!F) { @@ -457,7 +463,8 @@ const std::string Name = is64BitArch() ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_4"; Function *F = M->getFunction(Name); - StructType *IdentTy = M->getTypeByName("struct.ident_t"); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), "struct.ident_t"); // If F is not available, declare it. if (!F) { @@ -488,7 +495,8 @@ if (SourceLocDummy == nullptr) { const std::string StructName = "struct.ident_t"; - StructType *IdentTy = M->getTypeByName(StructName); + StructType *IdentTy = + StructType::getTypeByName(M->getContext(), StructName); // If the ident_t StructType is not available, declare it. // in LLVM-IR: ident_t = type { i32, i32, i32, i32, i8* }