From ad4bb8280952c2cacf497e30560ee94c119b36e0 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. --- llvm/include/llvm/IR/Type.h | 3 +++ llvm/lib/AsmParser/LLParser.cpp | 2 +- llvm/lib/IR/Type.cpp | 9 +++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h index 1cf4db5..430bc34 100644 --- a/llvm/include/llvm/IR/Type.h +++ b/llvm/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/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index be3b735..bcdf51c 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/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/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp index a7be014..a219989 100644 --- a/llvm/lib/IR/Type.cpp +++ b/llvm/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. -- 2.7.4