From 33e01a90452d44bf8a3fe32ef9fc7cd7990141a2 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 25 Jun 2021 20:55:09 +0200 Subject: [PATCH] [IR] Add Type::isOpaquePointerTy() helper (NFC) Shortcut to check for opaque pointers without a cast to PointerType. --- include/llvm/IR/Type.h | 3 +++ lib/AsmParser/LLParser.cpp | 2 +- lib/IR/Type.cpp | 9 +++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/llvm/IR/Type.h b/include/llvm/IR/Type.h index 1cf4db59908..430bc34a47e 100644 --- a/include/llvm/IR/Type.h +++ b/include/llvm/IR/Type.h @@ -227,6 +227,9 @@ public: /// True if this is an instance of PointerType. bool isPointerTy() const { return getTypeID() == PointerTyID; } + /// True if this is an instance of an opaque PointerType. + bool isOpaquePointerTy() const; + /// Return true if this is a pointer type or a vector of pointer types. bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); } diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index be3b7358270..bcdf51c47f9 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -2575,7 +2575,7 @@ bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) { } } - if (Result->isPointerTy() && cast(Result)->isOpaque()) { + if (Result->isOpaquePointerTy()) { unsigned AddrSpace; if (parseOptionalAddrSpace(AddrSpace)) return true; diff --git a/lib/IR/Type.cpp b/lib/IR/Type.cpp index a7be01415fb..a2199897606 100644 --- a/lib/IR/Type.cpp +++ b/lib/IR/Type.cpp @@ -60,6 +60,12 @@ bool Type::isIntegerTy(unsigned Bitwidth) const { return isIntegerTy() && cast(this)->getBitWidth() == Bitwidth; } +bool Type::isOpaquePointerTy() const { + if (auto *PTy = dyn_cast(this)) + return PTy->isOpaque(); + return false; +} + bool Type::canLosslesslyBitCastTo(Type *Ty) const { // Identity cast means no change so return true if (this == Ty) @@ -691,8 +697,7 @@ PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { LLVMContextImpl *CImpl = EltTy->getContext().pImpl; // Create opaque pointer for pointer to opaque pointer. - if (CImpl->ForceOpaquePointers || - (isa(EltTy) && cast(EltTy)->isOpaque())) + if (CImpl->ForceOpaquePointers || EltTy->isOpaquePointerTy()) return get(EltTy->getContext(), AddressSpace); // Since AddressSpace #0 is the common case, we special case it.