Author: Michele Scandale Date: 2025-04-22T21:29:07-07:00 New Revision: 34a4c58018730736b940c4db4d694feed3266f52
URL: https://github.com/llvm/llvm-project/commit/34a4c58018730736b940c4db4d694feed3266f52 DIFF: https://github.com/llvm/llvm-project/commit/34a4c58018730736b940c4db4d694feed3266f52.diff LOG: [clang] Rework `hasBooleanRepresentation`. (#136038) This is a follow-up of 13aac46332f607a38067b5ddd466071683b8c255. This commit adjusts the implementation of `hasBooleanRepresentation` to be somewhat aligned to `hasIntegerRepresentation`. In particular vector of booleans should be handled in `hasBooleanRepresentation`, while `_Atomic(bool)` should not. Added: Modified: clang/include/clang/AST/Type.h clang/lib/AST/Type.cpp clang/lib/CodeGen/CGExpr.cpp Removed: ################################################################################ diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index 20ff529c7e0c6..86d43e1a05b55 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2793,8 +2793,9 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase { /// of some sort, e.g., it is a floating-point type or a vector thereof. bool hasFloatingRepresentation() const; - /// Determine whether this type has a boolean representation - /// of some sort. + /// Determine whether this type has a boolean representation -- i.e., it is a + /// boolean type, an enum type whose underlying type is a boolean type, or a + /// vector of booleans. bool hasBooleanRepresentation() const; // Type Checking Functions: Check to see if this type is structurally the diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index 67cd690af7499..08be90eab6537 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -2346,16 +2346,15 @@ bool Type::isArithmeticType() const { } bool Type::hasBooleanRepresentation() const { - if (isBooleanType()) - return true; - - if (const EnumType *ET = getAs<EnumType>()) - return ET->getDecl()->getIntegerType()->isBooleanType(); - - if (const AtomicType *AT = getAs<AtomicType>()) - return AT->getValueType()->hasBooleanRepresentation(); - - return false; + if (const auto *VT = dyn_cast<VectorType>(CanonicalType)) + return VT->getElementType()->isBooleanType(); + if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) { + return ET->getDecl()->isComplete() && + ET->getDecl()->getIntegerType()->isBooleanType(); + } + if (const auto *IT = dyn_cast<BitIntType>(CanonicalType)) + return IT->getNumBits() == 1; + return isBooleanType(); } Type::ScalarTypeKind Type::getScalarTypeKind() const { diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index abb88477062fc..786a56eed7ed5 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -1920,7 +1920,7 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { llvm::APInt Min, End; if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums, - Ty->hasBooleanRepresentation())) + Ty->hasBooleanRepresentation() && !Ty->isVectorType())) return nullptr; llvm::MDBuilder MDHelper(getLLVMContext()); @@ -1948,7 +1948,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, if (!HasBoolCheck && !HasEnumCheck) return false; - bool IsBool = Ty->hasBooleanRepresentation() || + bool IsBool = (Ty->hasBooleanRepresentation() && !Ty->isVectorType()) || NSAPI(CGM.getContext()).isObjCBOOLType(Ty); bool NeedsBoolCheck = HasBoolCheck && IsBool; bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>(); @@ -2068,11 +2068,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, /// by ConvertType) to its load/store type (as returned by /// convertTypeForLoadStore). llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { - if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) { - llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType()); - bool Signed = Ty->isSignedIntegerOrEnumerationType(); - return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv"); - } + if (auto *AtomicTy = Ty->getAs<AtomicType>()) + Ty = AtomicTy->getValueType(); if (Ty->isExtVectorBoolType()) { llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType()); @@ -2088,6 +2085,12 @@ llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { Value = Builder.CreateBitCast(Value, StoreTy); } + if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) { + llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType()); + bool Signed = Ty->isSignedIntegerOrEnumerationType(); + return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv"); + } + return Value; } @@ -2095,6 +2098,9 @@ llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { /// by convertTypeForLoadStore) to its primary IR type (as returned /// by ConvertType). llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { + if (auto *AtomicTy = Ty->getAs<AtomicType>()) + Ty = AtomicTy->getValueType(); + if (Ty->isPackedVectorBoolType(getContext())) { const auto *RawIntTy = Value->getType(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits