Skip to content

Commit 3f0fe23

Browse files
committed
[clang][NFC] Refactor CodeGen's hasBooleanRepresentation
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 32f2402 commit 3f0fe23

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
@@ -2769,6 +2769,10 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
27692769
/// of some sort, e.g., it is a floating-point type or a vector thereof.
27702770
bool hasFloatingRepresentation() const;
27712771

2772+
/// Determine whether this type has a boolean representation
2773+
/// of some sort.
2774+
bool hasBooleanRepresentation() const;
2775+
27722776
// Type Checking Functions: Check to see if this type is structurally the
27732777
// specified type, ignoring typedefs and qualifiers, and return a pointer to
27742778
// 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
@@ -1893,19 +1893,6 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
18931893
lvalue.getTBAAInfo(), lvalue.isNontemporal());
18941894
}
18951895

1896-
static bool hasBooleanRepresentation(QualType Ty) {
1897-
if (Ty->isBooleanType())
1898-
return true;
1899-
1900-
if (const EnumType *ET = Ty->getAs<EnumType>())
1901-
return ET->getDecl()->getIntegerType()->isBooleanType();
1902-
1903-
if (const AtomicType *AT = Ty->getAs<AtomicType>())
1904-
return hasBooleanRepresentation(AT->getValueType());
1905-
1906-
return false;
1907-
}
1908-
19091896
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19101897
llvm::APInt &Min, llvm::APInt &End,
19111898
bool StrictEnums, bool IsBool) {
@@ -1928,7 +1915,7 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19281915
llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
19291916
llvm::APInt Min, End;
19301917
if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums,
1931-
hasBooleanRepresentation(Ty)))
1918+
Ty->hasBooleanRepresentation()))
19321919
return nullptr;
19331920

19341921
llvm::MDBuilder MDHelper(getLLVMContext());
@@ -1942,7 +1929,7 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19421929
if (!HasBoolCheck && !HasEnumCheck)
19431930
return false;
19441931

1945-
bool IsBool = hasBooleanRepresentation(Ty) ||
1932+
bool IsBool = Ty->hasBooleanRepresentation() ||
19461933
NSAPI(CGM.getContext()).isObjCBOOLType(Ty);
19471934
bool NeedsBoolCheck = HasBoolCheck && IsBool;
19481935
bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>();
@@ -2070,7 +2057,7 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
20702057
/// by ConvertType) to its load/store type (as returned by
20712058
/// convertTypeForLoadStore).
20722059
llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
2073-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType()) {
2060+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType()) {
20742061
llvm::Type *StoreTy = convertTypeForLoadStore(Ty, Value->getType());
20752062
bool Signed = Ty->isSignedIntegerOrEnumerationType();
20762063
return Builder.CreateIntCast(Value, StoreTy, Signed, "storedv");
@@ -2111,7 +2098,7 @@ llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
21112098
}
21122099

21132100
llvm::Type *ResTy = ConvertType(Ty);
2114-
if (hasBooleanRepresentation(Ty) || Ty->isBitIntType() ||
2101+
if (Ty->hasBooleanRepresentation() || Ty->isBitIntType() ||
21152102
Ty->isExtVectorBoolType())
21162103
return Builder.CreateTrunc(Value, ResTy, "loadedv");
21172104

@@ -2598,7 +2585,7 @@ void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
25982585
Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load");
25992586

26002587
// Mask the source value as needed.
2601-
if (!hasBooleanRepresentation(Dst.getType()))
2588+
if (!Dst.getType()->hasBooleanRepresentation())
26022589
SrcVal = Builder.CreateAnd(
26032590
SrcVal, llvm::APInt::getLowBitsSet(StorageSize, Info.Size),
26042591
"bf.value");

0 commit comments

Comments
 (0)