Author: Andy Kaylor Date: 2025-04-03T14:03:25-07:00 New Revision: 13aac46332f607a38067b5ddd466071683b8c255
URL: https://github.com/llvm/llvm-project/commit/13aac46332f607a38067b5ddd466071683b8c255 DIFF: https://github.com/llvm/llvm-project/commit/13aac46332f607a38067b5ddd466071683b8c255.diff LOG: [clang][NFC] Refactor CodeGen's hasBooleanRepresentation (#134159) The ClangIR upstreaming project needs the same logic for hasBooleanRepresentation() that is currently implemented in the standard clang codegen. In order to share this code, this change moves the implementation of this function into the AST Type class. No functional change is intended by this change. The ClangIR use of this function will be added separately in a later change. 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 06d60f618ddcb..9f6189440fabf 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -2760,6 +2760,10 @@ 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. + bool hasBooleanRepresentation() const; + // Type Checking Functions: Check to see if this type is structurally the // specified type, ignoring typedefs and qualifiers, and return a pointer to // the best type we can. diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index 879ad1a7eaa84..4336fe44b82ad 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -2334,6 +2334,19 @@ bool Type::isArithmeticType() const { return isa<ComplexType>(CanonicalType) || isBitIntType(); } +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; +} + Type::ScalarTypeKind Type::getScalarTypeKind() const { assert(isScalarType()); diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 73020389b5e45..0d7e5a2091146 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -1896,19 +1896,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, lvalue.getTBAAInfo(), lvalue.isNontemporal()); } -static bool hasBooleanRepresentation(QualType Ty) { - if (Ty->isBooleanType()) - return true; - - if (const EnumType *ET = Ty->getAs<EnumType>()) - return ET->getDecl()->getIntegerType()->isBooleanType(); - - if (const AtomicType *AT = Ty->getAs<AtomicType>()) - return hasBooleanRepresentation(AT->getValueType()); - - return false; -} - static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, llvm::APInt &Min, llvm::APInt &End, bool StrictEnums, bool IsBool) { @@ -1931,7 +1918,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, - hasBooleanRepresentation(Ty))) + Ty->hasBooleanRepresentation())) return nullptr; llvm::MDBuilder MDHelper(getLLVMContext()); @@ -1945,7 +1932,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, if (!HasBoolCheck && !HasEnumCheck) return false; - bool IsBool = hasBooleanRepresentation(Ty) || + bool IsBool = Ty->hasBooleanRepresentation() || NSAPI(CGM.getContext()).isObjCBOOLType(Ty); bool NeedsBoolCheck = HasBoolCheck && IsBool; bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>(); @@ -2073,7 +2060,7 @@ 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 (hasBooleanRepresentation(Ty) || Ty->isBitIntType()) { + if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) { llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType()); bool Signed = Ty->isSignedIntegerOrEnumerationType(); return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv"); @@ -2114,7 +2101,7 @@ llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { } llvm::Type *ResTy = ConvertType(Ty); - if (hasBooleanRepresentation(Ty) || Ty->isBitIntType() || + if (Ty->hasBooleanRepresentation() || Ty->isBitIntType() || Ty->isExtVectorBoolType()) return Builder.CreateTrunc(Value, ResTy, "loadedv"); @@ -2601,7 +2588,7 @@ void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load"); // Mask the source value as needed. - if (!hasBooleanRepresentation(Dst.getType())) + if (!Dst.getType()->hasBooleanRepresentation()) SrcVal = Builder.CreateAnd( SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size), "bf.value"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits