[OpaquePtrs] Add getNonOpaquePointerElementType() method (NFC)

This method is intended for use in places that cannot be reached
with opaque pointers, or part of deprecated methods. This makes
it easier to see that some uses of getPointerElementType() don't
need further action.

Differential Revision: https://reviews.llvm.org/D117870
This commit is contained in:
Nikita Popov 2022-01-21 12:48:31 +01:00
parent 3ad6de31c0
commit d29e319263
5 changed files with 32 additions and 24 deletions

View File

@ -366,7 +366,16 @@ public:
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");

View File

@ -1410,14 +1410,14 @@ static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) {
nullptr, GlobalVariable::NotThreadLocal,
PTy->getAddressSpace());
if (auto *FT = dyn_cast<FunctionType>(PTy->getPointerElementType()))
Type *ElemTy = PTy->getNonOpaquePointerElementType();
if (auto *FT = dyn_cast<FunctionType>(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)

View File

@ -1691,8 +1691,7 @@ LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
NumIndices);
Constant *Val = unwrap<Constant>(ConstantVal);
Type *Ty =
cast<PointerType>(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<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
NumIndices);
Constant *Val = unwrap<Constant>(ConstantVal);
Type *Ty =
cast<PointerType>(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<PointerType>(unwrap(Ty));
return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
PTy->getAddressSpace(),
GlobalValue::ExternalLinkage, Name,
unwrap<Constant>(Aliasee), unwrap(M)));
}
@ -3218,7 +3217,7 @@ LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
const char *Name) {
Value *V = unwrap(Fn);
FunctionType *FnT =
cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
cast<FunctionType>(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<PointerType>(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<Value *> IdxList(unwrap(Indices), NumIndices);
Value *Val = unwrap(Pointer);
Type *Ty =
cast<PointerType>(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<Value *> IdxList(unwrap(Indices), NumIndices);
Value *Val = unwrap(Pointer);
Type *Ty =
cast<PointerType>(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<PointerType>(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<FunctionType>(cast<PointerType>(V->getType())->getElementType());
cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
makeArrayRef(unwrap(Args), NumArgs), Name));

View File

@ -679,8 +679,8 @@ static void checkAsyncFuncPointer(const Instruction *I, Value *V) {
if (AsyncFuncPtrAddr->getType()->isOpaquePointerTy())
return;
auto *StructTy =
cast<StructType>(AsyncFuncPtrAddr->getType()->getPointerElementType());
auto *StructTy = cast<StructType>(
AsyncFuncPtrAddr->getType()->getNonOpaquePointerElementType());
if (StructTy->isOpaque() || !StructTy->isPacked() ||
StructTy->getNumElements() != 2 ||
!StructTy->getElementType(0)->isIntegerTy(32) ||

View File

@ -1437,8 +1437,10 @@ static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr,
if (Indices.size() == 1 && cast<ConstantInt>(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