diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h index c899c46d40554a..98c97375ad7b2e 100644 --- a/llvm/include/llvm/IR/Type.h +++ b/llvm/include/llvm/IR/Type.h @@ -366,7 +366,16 @@ class Type { return ContainedTys[0]; } + /// This method is deprecated without replacement. Pointer element types are + /// not available with opaque pointers. Type *getPointerElementType() const { + return getNonOpaquePointerElementType(); + } + + /// Only use this method in code that is not reachable with opaque pointers, + /// or part of deprecated methods that will be removed as part of the opaque + /// pointers transition. + Type *getNonOpaquePointerElementType() const { assert(getTypeID() == PointerTyID); assert(NumContainedTys && "Attempting to get element type of opaque pointer"); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index 18c1c31e101dcb..cec4ffd82f8189 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -1410,14 +1410,14 @@ static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) { nullptr, GlobalVariable::NotThreadLocal, PTy->getAddressSpace()); - if (auto *FT = dyn_cast(PTy->getPointerElementType())) + Type *ElemTy = PTy->getNonOpaquePointerElementType(); + if (auto *FT = dyn_cast(ElemTy)) return Function::Create(FT, GlobalValue::ExternalWeakLinkage, PTy->getAddressSpace(), "", M); else - return new GlobalVariable(*M, PTy->getPointerElementType(), false, - GlobalValue::ExternalWeakLinkage, nullptr, "", - nullptr, GlobalVariable::NotThreadLocal, - PTy->getAddressSpace()); + return new GlobalVariable( + *M, ElemTy, false, GlobalValue::ExternalWeakLinkage, nullptr, "", + nullptr, GlobalVariable::NotThreadLocal, PTy->getAddressSpace()); } Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty, @@ -5602,7 +5602,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine) { if (FRVI != ForwardRefVals.end()) { FwdFn = FRVI->second.first; if (!FwdFn->getType()->isOpaque()) { - if (!FwdFn->getType()->getPointerElementType()->isFunctionTy()) + if (!FwdFn->getType()->getNonOpaquePointerElementType()->isFunctionTy()) return error(FRVI->second.second, "invalid forward reference to " "function as global value!"); if (FwdFn->getType() != PFT) diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 9415e35a8f5124..3f899471843fc1 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -1691,8 +1691,7 @@ LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, ArrayRef IdxList(unwrap(ConstantIndices, NumIndices), NumIndices); Constant *Val = unwrap(ConstantVal); - Type *Ty = - cast(Val->getType()->getScalarType())->getElementType(); + Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType(); return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList)); } @@ -1710,8 +1709,7 @@ LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, ArrayRef IdxList(unwrap(ConstantIndices, NumIndices), NumIndices); Constant *Val = unwrap(ConstantVal); - Type *Ty = - cast(Val->getType()->getScalarType())->getElementType(); + Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType(); return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList)); } @@ -2278,7 +2276,8 @@ void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, const char *Name) { auto *PTy = cast(unwrap(Ty)); - return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), + return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(), + PTy->getAddressSpace(), GlobalValue::ExternalLinkage, Name, unwrap(Aliasee), unwrap(M))); } @@ -3218,7 +3217,7 @@ LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, const char *Name) { Value *V = unwrap(Fn); FunctionType *FnT = - cast(cast(V->getType())->getElementType()); + cast(V->getType()->getNonOpaquePointerElementType()); return wrap( unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch), @@ -3590,7 +3589,8 @@ LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, Value *V = unwrap(PointerVal); PointerType *Ty = cast(V->getType()); - return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name)); + return wrap( + unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name)); } LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty, @@ -3692,8 +3692,7 @@ LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, const char *Name) { ArrayRef IdxList(unwrap(Indices), NumIndices); Value *Val = unwrap(Pointer); - Type *Ty = - cast(Val->getType()->getScalarType())->getElementType(); + Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType(); return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name)); } @@ -3709,8 +3708,7 @@ LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, const char *Name) { ArrayRef IdxList(unwrap(Indices), NumIndices); Value *Val = unwrap(Pointer); - Type *Ty = - cast(Val->getType()->getScalarType())->getElementType(); + Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType(); return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name)); } @@ -3725,8 +3723,7 @@ LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, unsigned Idx, const char *Name) { Value *Val = unwrap(Pointer); - Type *Ty = - cast(Val->getType()->getScalarType())->getElementType(); + Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType(); return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name)); } @@ -3947,7 +3944,7 @@ LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, const char *Name) { Value *V = unwrap(Fn); FunctionType *FnT = - cast(cast(V->getType())->getElementType()); + cast(V->getType()->getNonOpaquePointerElementType()); return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Name)); diff --git a/llvm/lib/Transforms/Coroutines/Coroutines.cpp b/llvm/lib/Transforms/Coroutines/Coroutines.cpp index a8123aee319ef9..965a146c143fac 100644 --- a/llvm/lib/Transforms/Coroutines/Coroutines.cpp +++ b/llvm/lib/Transforms/Coroutines/Coroutines.cpp @@ -679,8 +679,8 @@ static void checkAsyncFuncPointer(const Instruction *I, Value *V) { if (AsyncFuncPtrAddr->getType()->isOpaquePointerTy()) return; - auto *StructTy = - cast(AsyncFuncPtrAddr->getType()->getPointerElementType()); + auto *StructTy = cast( + AsyncFuncPtrAddr->getType()->getNonOpaquePointerElementType()); if (StructTy->isOpaque() || !StructTy->isPacked() || StructTy->getNumElements() != 2 || !StructTy->getElementType(0)->isIntegerTy(32) || diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 1b0290cf5709d0..2ed87ce6295b3c 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -1437,8 +1437,10 @@ static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr, if (Indices.size() == 1 && cast(Indices.back())->isZero()) return BasePtr; - return IRB.CreateInBoundsGEP(BasePtr->getType()->getPointerElementType(), - BasePtr, Indices, NamePrefix + "sroa_idx"); + // buildGEP() is only called for non-opaque pointers. + return IRB.CreateInBoundsGEP( + BasePtr->getType()->getNonOpaquePointerElementType(), BasePtr, Indices, + NamePrefix + "sroa_idx"); } /// Get a natural GEP off of the BasePtr walking through Ty toward