Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,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.
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
23 changes: 5 additions & 18 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,19 +1893,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) {
Expand All @@ -1928,7 +1915,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());
Expand All @@ -1942,7 +1929,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>();
Expand Down Expand Up @@ -2070,7 +2057,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");
Expand Down Expand Up @@ -2111,7 +2098,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");

Expand Down Expand Up @@ -2598,7 +2585,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");
Expand Down