Skip to content

Commit d29e319

Browse files
committed
[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
1 parent 3ad6de3 commit d29e319

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

llvm/include/llvm/IR/Type.h

+9
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,16 @@ class Type {
366366
return ContainedTys[0];
367367
}
368368

369+
/// This method is deprecated without replacement. Pointer element types are
370+
/// not available with opaque pointers.
369371
Type *getPointerElementType() const {
372+
return getNonOpaquePointerElementType();
373+
}
374+
375+
/// Only use this method in code that is not reachable with opaque pointers,
376+
/// or part of deprecated methods that will be removed as part of the opaque
377+
/// pointers transition.
378+
Type *getNonOpaquePointerElementType() const {
370379
assert(getTypeID() == PointerTyID);
371380
assert(NumContainedTys &&
372381
"Attempting to get element type of opaque pointer");

llvm/lib/AsmParser/LLParser.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1410,14 +1410,14 @@ static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) {
14101410
nullptr, GlobalVariable::NotThreadLocal,
14111411
PTy->getAddressSpace());
14121412

1413-
if (auto *FT = dyn_cast<FunctionType>(PTy->getPointerElementType()))
1413+
Type *ElemTy = PTy->getNonOpaquePointerElementType();
1414+
if (auto *FT = dyn_cast<FunctionType>(ElemTy))
14141415
return Function::Create(FT, GlobalValue::ExternalWeakLinkage,
14151416
PTy->getAddressSpace(), "", M);
14161417
else
1417-
return new GlobalVariable(*M, PTy->getPointerElementType(), false,
1418-
GlobalValue::ExternalWeakLinkage, nullptr, "",
1419-
nullptr, GlobalVariable::NotThreadLocal,
1420-
PTy->getAddressSpace());
1418+
return new GlobalVariable(
1419+
*M, ElemTy, false, GlobalValue::ExternalWeakLinkage, nullptr, "",
1420+
nullptr, GlobalVariable::NotThreadLocal, PTy->getAddressSpace());
14211421
}
14221422

14231423
Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
@@ -5602,7 +5602,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine) {
56025602
if (FRVI != ForwardRefVals.end()) {
56035603
FwdFn = FRVI->second.first;
56045604
if (!FwdFn->getType()->isOpaque()) {
5605-
if (!FwdFn->getType()->getPointerElementType()->isFunctionTy())
5605+
if (!FwdFn->getType()->getNonOpaquePointerElementType()->isFunctionTy())
56065606
return error(FRVI->second.second, "invalid forward reference to "
56075607
"function as global value!");
56085608
if (FwdFn->getType() != PFT)

llvm/lib/IR/Core.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -1691,8 +1691,7 @@ LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
16911691
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
16921692
NumIndices);
16931693
Constant *Val = unwrap<Constant>(ConstantVal);
1694-
Type *Ty =
1695-
cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1694+
Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
16961695
return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
16971696
}
16981697

@@ -1710,8 +1709,7 @@ LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
17101709
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
17111710
NumIndices);
17121711
Constant *Val = unwrap<Constant>(ConstantVal);
1713-
Type *Ty =
1714-
cast<PointerType>(Val->getType()->getScalarType())->getElementType();
1712+
Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
17151713
return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
17161714
}
17171715

@@ -2278,7 +2276,8 @@ void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
22782276
LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
22792277
const char *Name) {
22802278
auto *PTy = cast<PointerType>(unwrap(Ty));
2281-
return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
2279+
return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
2280+
PTy->getAddressSpace(),
22822281
GlobalValue::ExternalLinkage, Name,
22832282
unwrap<Constant>(Aliasee), unwrap(M)));
22842283
}
@@ -3218,7 +3217,7 @@ LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
32183217
const char *Name) {
32193218
Value *V = unwrap(Fn);
32203219
FunctionType *FnT =
3221-
cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3220+
cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
32223221

32233222
return wrap(
32243223
unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
@@ -3590,7 +3589,8 @@ LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
35903589
Value *V = unwrap(PointerVal);
35913590
PointerType *Ty = cast<PointerType>(V->getType());
35923591

3593-
return wrap(unwrap(B)->CreateLoad(Ty->getElementType(), V, Name));
3592+
return wrap(
3593+
unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name));
35943594
}
35953595

35963596
LLVMValueRef LLVMBuildLoad2(LLVMBuilderRef B, LLVMTypeRef Ty,
@@ -3692,8 +3692,7 @@ LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
36923692
const char *Name) {
36933693
ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
36943694
Value *Val = unwrap(Pointer);
3695-
Type *Ty =
3696-
cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3695+
Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
36973696
return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
36983697
}
36993698

@@ -3709,8 +3708,7 @@ LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
37093708
const char *Name) {
37103709
ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
37113710
Value *Val = unwrap(Pointer);
3712-
Type *Ty =
3713-
cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3711+
Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
37143712
return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
37153713
}
37163714

@@ -3725,8 +3723,7 @@ LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
37253723
LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
37263724
unsigned Idx, const char *Name) {
37273725
Value *Val = unwrap(Pointer);
3728-
Type *Ty =
3729-
cast<PointerType>(Val->getType()->getScalarType())->getElementType();
3726+
Type *Ty = Val->getType()->getScalarType()->getNonOpaquePointerElementType();
37303727
return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
37313728
}
37323729

@@ -3947,7 +3944,7 @@ LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
39473944
const char *Name) {
39483945
Value *V = unwrap(Fn);
39493946
FunctionType *FnT =
3950-
cast<FunctionType>(cast<PointerType>(V->getType())->getElementType());
3947+
cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
39513948

39523949
return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn),
39533950
makeArrayRef(unwrap(Args), NumArgs), Name));

llvm/lib/Transforms/Coroutines/Coroutines.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ static void checkAsyncFuncPointer(const Instruction *I, Value *V) {
679679
if (AsyncFuncPtrAddr->getType()->isOpaquePointerTy())
680680
return;
681681

682-
auto *StructTy =
683-
cast<StructType>(AsyncFuncPtrAddr->getType()->getPointerElementType());
682+
auto *StructTy = cast<StructType>(
683+
AsyncFuncPtrAddr->getType()->getNonOpaquePointerElementType());
684684
if (StructTy->isOpaque() || !StructTy->isPacked() ||
685685
StructTy->getNumElements() != 2 ||
686686
!StructTy->getElementType(0)->isIntegerTy(32) ||

llvm/lib/Transforms/Scalar/SROA.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1437,8 +1437,10 @@ static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr,
14371437
if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero())
14381438
return BasePtr;
14391439

1440-
return IRB.CreateInBoundsGEP(BasePtr->getType()->getPointerElementType(),
1441-
BasePtr, Indices, NamePrefix + "sroa_idx");
1440+
// buildGEP() is only called for non-opaque pointers.
1441+
return IRB.CreateInBoundsGEP(
1442+
BasePtr->getType()->getNonOpaquePointerElementType(), BasePtr, Indices,
1443+
NamePrefix + "sroa_idx");
14421444
}
14431445

14441446
/// Get a natural GEP off of the BasePtr walking through Ty toward

0 commit comments

Comments
 (0)