diff --git a/llvm/include/llvm/Support/Casting.h b/llvm/include/llvm/Support/Casting.h index 66fdcb44ea2c0..2ce70e732e2ec 100644 --- a/llvm/include/llvm/Support/Casting.h +++ b/llvm/include/llvm/Support/Casting.h @@ -614,12 +614,12 @@ template struct ValueIsPresent> { static inline decltype(auto) unwrapValue(std::optional &t) { return *t; } }; -// If something is "nullable" then we just compare it to nullptr to see if it -// exists. +// If something is "nullable" then we just cast it to bool to see if it exists. template -struct ValueIsPresent>> { +struct ValueIsPresent< + T, std::enable_if_t && std::is_constructible_v>> { using UnwrappedType = T; - static inline bool isPresent(const T &t) { return t != T(nullptr); } + static inline bool isPresent(const T &t) { return static_cast(t); } static inline decltype(auto) unwrapValue(T &t) { return t; } }; diff --git a/llvm/lib/CodeGen/RegisterBankInfo.cpp b/llvm/lib/CodeGen/RegisterBankInfo.cpp index e1720b038e236..5a8cf13ad11fd 100644 --- a/llvm/lib/CodeGen/RegisterBankInfo.cpp +++ b/llvm/lib/CodeGen/RegisterBankInfo.cpp @@ -134,10 +134,10 @@ const TargetRegisterClass *RegisterBankInfo::constrainGenericRegister( // If the register already has a class, fallback to MRI::constrainRegClass. auto &RegClassOrBank = MRI.getRegClassOrRegBank(Reg); - if (isa(RegClassOrBank)) + if (isa_and_present(RegClassOrBank)) return MRI.constrainRegClass(Reg, &RC); - const RegisterBank *RB = cast(RegClassOrBank); + const auto *RB = dyn_cast_if_present(RegClassOrBank); // Otherwise, all we can do is ensure the bank covers the class, and set it. if (RB && !RB->covers(RC)) return nullptr; diff --git a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp index 704435dad65d7..8fa656c77e90e 100644 --- a/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp +++ b/llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp @@ -3708,10 +3708,10 @@ const TargetRegisterClass * SIRegisterInfo::getConstrainedRegClassForOperand(const MachineOperand &MO, const MachineRegisterInfo &MRI) const { const RegClassOrRegBank &RCOrRB = MRI.getRegClassOrRegBank(MO.getReg()); - if (const RegisterBank *RB = dyn_cast(RCOrRB)) + if (const auto *RB = dyn_cast_if_present(RCOrRB)) return getRegClassForTypeOnBank(MRI.getType(MO.getReg()), *RB); - if (const auto *RC = dyn_cast(RCOrRB)) + if (const auto *RC = dyn_cast_if_present(RCOrRB)) return getAllocatableClass(RC); return nullptr; diff --git a/llvm/unittests/ADT/PointerUnionTest.cpp b/llvm/unittests/ADT/PointerUnionTest.cpp index acddb78960149..a28d532865cbc 100644 --- a/llvm/unittests/ADT/PointerUnionTest.cpp +++ b/llvm/unittests/ADT/PointerUnionTest.cpp @@ -208,6 +208,11 @@ TEST_F(PointerUnionTest, NewCastInfra) { EXPECT_FALSE(isa(d4null)); EXPECT_FALSE(isa(d4null)); + EXPECT_FALSE(isa_and_present(i4null)); + EXPECT_FALSE(isa_and_present(f4null)); + EXPECT_FALSE(isa_and_present(l4null)); + EXPECT_FALSE(isa_and_present(d4null)); + // test cast<> EXPECT_EQ(cast(a), &f); EXPECT_EQ(cast(b), &i);