Skip to content

Commit 13aac46

Browse files
authored
[clang][NFC] Refactor CodeGen's hasBooleanRepresentation (llvm#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.
1 parent 4f902d2 commit 13aac46

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

clang/include/clang/AST/Type.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,10 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
27602760
/// of some sort, e.g., it is a floating-point type or a vector thereof.
27612761
bool hasFloatingRepresentation() const;
27622762

2763+
/// Determine whether this type has a boolean representation
2764+
/// of some sort.
2765+
bool hasBooleanRepresentation() const;
2766+
27632767
// Type Checking Functions: Check to see if this type is structurally the
27642768
// specified type, ignoring typedefs and qualifiers, and return a pointer to
27652769
// the best type we can.

clang/lib/AST/Type.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,19 @@ bool Type::isArithmeticType() const {
23342334
return isa<ComplexType>(CanonicalType) || isBitIntType();
23352335
}
23362336

2337+
bool Type::hasBooleanRepresentation() const {
2338+
if (isBooleanType())
2339+
return true;
2340+
2341+
if (const EnumType *ET = getAs<EnumType>())
2342+
return ET->getDecl()->getIntegerType()->isBooleanType();
2343+
2344+
if (const AtomicType *AT = getAs<AtomicType>())
2345+
return AT->getValueType()->hasBooleanRepresentation();
2346+
2347+
return false;
2348+
}
2349+
23372350
Type::ScalarTypeKind Type::getScalarTypeKind() const {
23382351
assert(isScalarType());
23392352

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,19 +1896,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
18961896
lvalue.getTBAAInfo(), lvalue.isNontemporal());
18971897
}
18981898

1899-
static bool hasBooleanRepresentation(QualType Ty) {
1900-
if (Ty->isBooleanType())
1901-
return true;
1902-
1903-
if (const EnumType *ET = Ty->getAs<EnumType>())
1904-
return ET->getDecl()->getIntegerType()->isBooleanType();
1905-
1906-
if (const AtomicType *AT = Ty->getAs<AtomicType>())
1907-
return hasBooleanRepresentation(AT->getValueType());
1908-
1909-
return false;
1910-
}
1911-
19121899
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19131900
llvm::APInt &Min, llvm::APInt &End,
19141901
bool StrictEnums, bool IsBool) {
@@ -1931,7 +1918,7 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19311918
llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
19321919
llvm::APInt Min, End;
19331920
if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
1934-
hasBooleanRepresentation(Ty)))
1921+
Ty->hasBooleanRepresentation()))
19351922
return nullptr;
19361923

19371924
llvm::MDBuilder MDHelper(getLLVMContext());
@@ -1945,7 +1932,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19451932
if (!HasBoolCheck && !HasEnumCheck)
19461933
return false;
19471934

1948-
bool IsBool = hasBooleanRepresentation(Ty) ||
1935+
bool IsBool = Ty->hasBooleanRepresentation() ||
19491936
NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
19501937
bool NeedsBoolCheck = HasBoolCheck && IsBool;
19511938
bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
@@ -2073,7 +2060,7 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
20732060
/// by ConvertType) to its load/store type (as returned by
20742061
/// convertTypeForLoadStore).
20752062
llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
2076-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType()) {
2063+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
20772064
llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
20782065
bool Signed = Ty->isSignedIntegerOrEnumerationType();
20792066
return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
@@ -2114,7 +2101,7 @@ llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
21142101
}
21152102

21162103
llvm::Type *ResTy = ConvertType(Ty);
2117-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType() ||
2104+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType() ||
21182105
Ty->isExtVectorBoolType())
21192106
return Builder.CreateTrunc(Value, ResTy, "loadedv");
21202107

@@ -2601,7 +2588,7 @@ void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
26012588
Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
26022589

26032590
// Mask the source value as needed.
2604-
if (!hasBooleanRepresentation(Dst.getType()))
2591+
if (!Dst.getType()->hasBooleanRepresentation())
26052592
SrcVal = Builder.CreateAnd(
26062593
SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
26072594
"bf.value");

0 commit comments

Comments
 (0)