Skip to content

Commit 53485b1

Browse files
committed
Store PtrAuth inside Mask to enhance performance
1 parent 1d672ba commit 53485b1

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

clang/include/clang/AST/Type.h

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include <cstddef>
5353
#include <cstdint>
5454
#include <cstring>
55-
#include <limits>
5655
#include <optional>
5756
#include <string>
5857
#include <type_traits>
@@ -211,6 +210,8 @@ class PointerAuthQualifier {
211210
(AuthenticatesNullValues << AuthenticatesNullValuesShift)) {
212211
assert(Key <= KeyNoneInternal);
213212
assert(ExtraDiscriminator <= MaxDiscriminator);
213+
assert((Data == 0) ==
214+
(getAuthenticationMode() == PointerAuthenticationMode::None));
214215
}
215216

216217
public:
@@ -240,7 +241,9 @@ class PointerAuthQualifier {
240241
}
241242

242243
bool isPresent() const {
243-
return getAuthenticationMode() != PointerAuthenticationMode::None;
244+
assert((Data == 0) ==
245+
(getAuthenticationMode() == PointerAuthenticationMode::None));
246+
return Data != 0;
244247
}
245248

246249
explicit operator bool() const { return isPresent(); }
@@ -298,6 +301,8 @@ class PointerAuthQualifier {
298301
static PointerAuthQualifier fromOpaqueValue(uint32_t Opaque) {
299302
PointerAuthQualifier Result;
300303
Result.Data = Opaque;
304+
assert((Result.Data == 0) ==
305+
(Result.getAuthenticationMode() == PointerAuthenticationMode::None));
301306
return Result;
302307
}
303308

@@ -312,8 +317,9 @@ class PointerAuthQualifier {
312317
/// * Objective C: the GC attributes (none, weak, or strong)
313318
class Qualifiers {
314319
public:
315-
enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ.
316-
Const = 0x1,
320+
enum TQ : uint64_t {
321+
// NOTE: These flags must be kept in sync with DeclSpec::TQ.
322+
Const = 0x1,
317323
Restrict = 0x2,
318324
Volatile = 0x4,
319325
CVRMask = Const | Volatile | Restrict
@@ -347,7 +353,7 @@ class Qualifiers {
347353
OCL_Autoreleasing
348354
};
349355

350-
enum {
356+
enum : uint64_t {
351357
/// The maximum supported address space number.
352358
/// 23 bits should be enough for anyone.
353359
MaxAddressSpace = 0x7fffffu,
@@ -363,8 +369,11 @@ class Qualifiers {
363369
/// the given sets.
364370
static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) {
365371
Qualifiers Q;
366-
if (L.getPointerAuth().isEquivalent(R.getPointerAuth())) {
367-
Q.setPointerAuth(L.getPointerAuth().withoutKeyNone());
372+
PointerAuthQualifier LPtrAuth = L.getPointerAuth();
373+
if (LPtrAuth.isPresent() &&
374+
LPtrAuth.getKey() != PointerAuthQualifier::KeyNoneInternal &&
375+
LPtrAuth == R.getPointerAuth()) {
376+
Q.setPointerAuth(LPtrAuth);
368377
PointerAuthQualifier Empty;
369378
L.setPointerAuth(Empty);
370379
R.setPointerAuth(Empty);
@@ -422,18 +431,14 @@ class Qualifiers {
422431
}
423432

424433
// Deserialize qualifiers from an opaque representation.
425-
static Qualifiers fromOpaqueValue(uint64_t Opaque) {
434+
static Qualifiers fromOpaqueValue(uint64_t opaque) {
426435
Qualifiers Qs;
427-
constexpr uint32_t U32Max = std::numeric_limits<uint32_t>::max();
428-
Qs.Mask = Opaque & U32Max;
429-
Qs.PtrAuth = PointerAuthQualifier::fromOpaqueValue((Opaque >> 32) & U32Max);
436+
Qs.Mask = opaque;
430437
return Qs;
431438
}
432439

433440
// Serialize these qualifiers into an opaque representation.
434-
uint64_t getAsOpaqueValue() const {
435-
return uint64_t(Mask) | (uint64_t(PtrAuth.getAsOpaqueValue()) << 32);
436-
}
441+
uint64_t getAsOpaqueValue() const { return Mask; }
437442

438443
bool hasConst() const { return Mask & Const; }
439444
bool hasOnlyConst() const { return Mask == Const; }
@@ -580,9 +585,19 @@ class Qualifiers {
580585
setAddressSpace(space);
581586
}
582587

583-
PointerAuthQualifier getPointerAuth() const { return PtrAuth; }
584-
void setPointerAuth(PointerAuthQualifier q) { PtrAuth = q; }
585-
void removePtrAuth() { PtrAuth = PointerAuthQualifier(); }
588+
bool hasPointerAuth() const { return Mask & PtrAuthMask; }
589+
PointerAuthQualifier getPointerAuth() const {
590+
return PointerAuthQualifier::fromOpaqueValue(Mask >> PtrAuthShift);
591+
}
592+
void setPointerAuth(PointerAuthQualifier Q) {
593+
Mask = (Mask & ~PtrAuthMask) |
594+
(uint64_t(Q.getAsOpaqueValue()) << PtrAuthShift);
595+
}
596+
void removePointerAuth() { Mask &= ~PtrAuthMask; }
597+
void addPointerAuth(PointerAuthQualifier Q) {
598+
assert(Q.isPresent());
599+
setPointerAuth(Q);
600+
}
586601

587602
// Fast qualifiers are those that can be allocated directly
588603
// on a QualType object.
@@ -606,16 +621,16 @@ class Qualifiers {
606621

607622
/// Return true if the set contains any qualifiers which require an ExtQuals
608623
/// node to be allocated.
609-
bool hasNonFastQualifiers() const { return (Mask & ~FastMask) || PtrAuth; }
624+
bool hasNonFastQualifiers() const { return Mask & ~FastMask; }
610625
Qualifiers getNonFastQualifiers() const {
611626
Qualifiers Quals = *this;
612627
Quals.setFastQualifiers(0);
613628
return Quals;
614629
}
615630

616631
/// Return true if the set contains any qualifiers.
617-
bool hasQualifiers() const { return Mask || PtrAuth; }
618-
bool empty() const { return !hasQualifiers(); }
632+
bool hasQualifiers() const { return Mask; }
633+
bool empty() const { return !Mask; }
619634

620635
/// Add the qualifiers from the given set to this set.
621636
void addQualifiers(Qualifiers Q) {
@@ -631,10 +646,9 @@ class Qualifiers {
631646
addObjCGCAttr(Q.getObjCGCAttr());
632647
if (Q.hasObjCLifetime())
633648
addObjCLifetime(Q.getObjCLifetime());
649+
if (Q.hasPointerAuth())
650+
addPointerAuth(Q.getPointerAuth());
634651
}
635-
636-
if (Q.PtrAuth)
637-
PtrAuth = Q.PtrAuth;
638652
}
639653

640654
/// Remove the qualifiers from the given set from this set.
@@ -651,10 +665,9 @@ class Qualifiers {
651665
removeObjCLifetime();
652666
if (getAddressSpace() == Q.getAddressSpace())
653667
removeAddressSpace();
668+
if (getPointerAuth() == Q.getPointerAuth())
669+
removePointerAuth();
654670
}
655-
656-
if (PtrAuth == Q.PtrAuth)
657-
PtrAuth = PointerAuthQualifier();
658671
}
659672

660673
/// Add the qualifiers from the given set to this set, given that
@@ -666,10 +679,9 @@ class Qualifiers {
666679
!hasObjCGCAttr() || !qs.hasObjCGCAttr());
667680
assert(getObjCLifetime() == qs.getObjCLifetime() ||
668681
!hasObjCLifetime() || !qs.hasObjCLifetime());
669-
assert(!PtrAuth || !qs.PtrAuth || PtrAuth == qs.PtrAuth);
682+
assert(!hasPointerAuth() || !qs.hasPointerAuth() ||
683+
getPointerAuth() == qs.getPointerAuth());
670684
Mask |= qs.Mask;
671-
if (qs.PtrAuth)
672-
PtrAuth = qs.PtrAuth;
673685
}
674686

675687
/// Returns true if address space A is equal to or a superset of B.
@@ -723,7 +735,7 @@ class Qualifiers {
723735
(getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() ||
724736
!other.hasObjCGCAttr()) &&
725737
// Pointer-auth qualifiers must match exactly.
726-
PtrAuth == other.PtrAuth &&
738+
getPointerAuth() == other.getPointerAuth() &&
727739
// ObjC lifetime qualifiers must match exactly.
728740
getObjCLifetime() == other.getObjCLifetime() &&
729741
// CVR qualifiers may subset.
@@ -756,12 +768,8 @@ class Qualifiers {
756768
/// another set of qualifiers, not considering qualifier compatibility.
757769
bool isStrictSupersetOf(Qualifiers Other) const;
758770

759-
bool operator==(Qualifiers Other) const {
760-
return Mask == Other.Mask && PtrAuth == Other.PtrAuth;
761-
}
762-
bool operator!=(Qualifiers Other) const {
763-
return Mask != Other.Mask || PtrAuth != Other.PtrAuth;
764-
}
771+
bool operator==(Qualifiers Other) const { return Mask == Other.Mask; }
772+
bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; }
765773

766774
explicit operator bool() const { return hasQualifiers(); }
767775

@@ -797,28 +805,26 @@ class Qualifiers {
797805
void print(raw_ostream &OS, const PrintingPolicy &Policy,
798806
bool appendSpaceIfNonEmpty = false) const;
799807

800-
void Profile(llvm::FoldingSetNodeID &ID) const {
801-
ID.AddInteger(Mask);
802-
PtrAuth.Profile(ID);
803-
}
808+
void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(Mask); }
804809

805810
private:
806-
// bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31|
807-
// |C R V|U|GCAttr|Lifetime|AddressSpace|
808-
uint32_t Mask = 0;
809-
810-
PointerAuthQualifier PtrAuth;
811+
// bits: |0 1 2|3|4 .. 5|6 .. 8|9 ... 31|32 ... 63|
812+
// |C R V|U|GCAttr|Lifetime|AddressSpace| PtrAuth |
813+
uint64_t Mask = 0;
811814
static_assert(sizeof(PointerAuthQualifier) == sizeof(uint32_t),
812815
"PointerAuthQualifier must be 32 bits");
813-
static const uint32_t UMask = 0x8;
814-
static const uint32_t UShift = 3;
815-
static const uint32_t GCAttrMask = 0x30;
816-
static const uint32_t GCAttrShift = 4;
817-
static const uint32_t LifetimeMask = 0x1C0;
818-
static const uint32_t LifetimeShift = 6;
819-
static const uint32_t AddressSpaceMask =
816+
817+
static constexpr uint64_t UMask = 0x8;
818+
static constexpr uint64_t UShift = 3;
819+
static constexpr uint64_t GCAttrMask = 0x30;
820+
static constexpr uint64_t GCAttrShift = 4;
821+
static constexpr uint64_t LifetimeMask = 0x1C0;
822+
static constexpr uint64_t LifetimeShift = 6;
823+
static constexpr uint64_t AddressSpaceMask =
820824
~(CVRMask | UMask | GCAttrMask | LifetimeMask);
821-
static const uint32_t AddressSpaceShift = 9;
825+
static constexpr uint64_t AddressSpaceShift = 9;
826+
static constexpr uint64_t PtrAuthShift = 32;
827+
static constexpr uint64_t PtrAuthMask = uint64_t(0xffffffff) << PtrAuthShift;
822828
};
823829

824830
class QualifiersAndAtomic {

0 commit comments

Comments
 (0)