Skip to content

Commit 4086a51

Browse files
committed
[CodeGen] Stop storing alignment information into pointers in Address
This reverts b1613f0. The change was made in an attempt to reduce memory consumption by storing the alignment information into pointers, but it turns out it doesn't make much difference. https://llvm-compile-time-tracker.com/compare.php?from=998ad085e865f2e5acc589d6bee0e3379042da2e&to=5de4a1989c474f37ac03f20ccb0aef50f6e3b854&stat=max-rss This fixes a bug introduced in https://reviews.llvm.org/D142584. The patch reduced the number of bits used for alignment from 6 bits to 5 bits, which made it impossible to encode the maximum allowed alignment in llvm (1 << 32). Differential Revision: https://reviews.llvm.org/D144686 (cherry picked from commit e419e22)
1 parent 2897dab commit 4086a51

File tree

1 file changed

+13
-71
lines changed

1 file changed

+13
-71
lines changed

clang/lib/CodeGen/Address.h

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,80 +25,20 @@ namespace CodeGen {
2525
// Indicates whether a pointer is known not to be null.
2626
enum KnownNonNull_t { NotKnownNonNull, KnownNonNull };
2727

28-
// We try to save some space by using 6 bits over two PointerIntPairs to store
29-
// the alignment. However, some arches don't support 3 bits in a PointerIntPair
30-
// so we fallback to storing the alignment separately.
31-
template <typename T, bool = alignof(llvm::Value *) >= 8> class AddressImpl {};
32-
33-
template <typename T> class AddressImpl<T, false> {
28+
/// An aligned address.
29+
class Address {
3430
llvm::PointerIntPair<llvm::Value *, 1, bool> PointerAndKnownNonNull;
3531
llvm::Type *ElementType;
3632
CharUnits Alignment;
3733

38-
public:
39-
AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType,
40-
CharUnits Alignment, KnownNonNull_t IsKnownNonNull)
41-
: PointerAndKnownNonNull(Pointer, IsKnownNonNull),
42-
ElementType(ElementType), Alignment(Alignment) {}
43-
llvm::Value *getPointer() const {
44-
return PointerAndKnownNonNull.getPointer();
45-
}
46-
llvm::Type *getElementType() const { return ElementType; }
47-
CharUnits getAlignment() const { return Alignment; }
48-
KnownNonNull_t isKnownNonNull() const {
49-
return (KnownNonNull_t)PointerAndKnownNonNull.getInt();
50-
}
51-
void setKnownNonNull() { PointerAndKnownNonNull.setInt(true); }
52-
};
53-
54-
template <typename T> class AddressImpl<T, true> {
55-
// Int portion stores the non-null bit and the upper 2 bits of the log of the
56-
// alignment.
57-
llvm::PointerIntPair<llvm::Value *, 3, unsigned> Pointer;
58-
// Int portion stores lower 3 bits of the log of the alignment.
59-
llvm::PointerIntPair<llvm::Type *, 3, unsigned> ElementType;
60-
61-
public:
62-
AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType,
63-
CharUnits Alignment, KnownNonNull_t IsKnownNonNull)
64-
: Pointer(Pointer), ElementType(ElementType) {
65-
if (Alignment.isZero()) {
66-
this->Pointer.setInt(IsKnownNonNull << 2);
67-
return;
68-
}
69-
// Currently the max supported alignment is exactly 1 << 32 and is
70-
// guaranteed to be a power of 2, so we can store the log of the alignment
71-
// into 5 bits.
72-
assert(Alignment.isPowerOfTwo() && "Alignment cannot be zero");
73-
auto AlignLog = llvm::Log2_64(Alignment.getQuantity());
74-
assert(AlignLog < (1 << 5) && "cannot fit alignment into 5 bits");
75-
this->Pointer.setInt(IsKnownNonNull << 2 | AlignLog >> 3);
76-
this->ElementType.setInt(AlignLog & 7);
77-
}
78-
llvm::Value *getPointer() const { return Pointer.getPointer(); }
79-
llvm::Type *getElementType() const { return ElementType.getPointer(); }
80-
CharUnits getAlignment() const {
81-
unsigned AlignLog = ((Pointer.getInt() & 0x3) << 3) | ElementType.getInt();
82-
return CharUnits::fromQuantity(CharUnits::QuantityType(1) << AlignLog);
83-
}
84-
KnownNonNull_t isKnownNonNull() const {
85-
return (KnownNonNull_t)(!!(Pointer.getInt() & 0x4));
86-
}
87-
void setKnownNonNull() { Pointer.setInt(Pointer.getInt() | 0x4); }
88-
};
89-
90-
/// An aligned address.
91-
class Address {
92-
AddressImpl<void> A;
93-
9434
protected:
95-
Address(std::nullptr_t)
96-
: A(nullptr, nullptr, CharUnits::Zero(), NotKnownNonNull) {}
35+
Address(std::nullptr_t) : ElementType(nullptr) {}
9736

9837
public:
9938
Address(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment,
10039
KnownNonNull_t IsKnownNonNull = NotKnownNonNull)
101-
: A(Pointer, ElementType, Alignment, IsKnownNonNull) {
40+
: PointerAndKnownNonNull(Pointer, IsKnownNonNull),
41+
ElementType(ElementType), Alignment(Alignment) {
10242
assert(Pointer != nullptr && "Pointer cannot be null");
10343
assert(ElementType != nullptr && "Element type cannot be null");
10444
assert(llvm::cast<llvm::PointerType>(Pointer->getType())
@@ -107,11 +47,13 @@ class Address {
10747
}
10848

10949
static Address invalid() { return Address(nullptr); }
110-
bool isValid() const { return A.getPointer() != nullptr; }
50+
bool isValid() const {
51+
return PointerAndKnownNonNull.getPointer() != nullptr;
52+
}
11153

11254
llvm::Value *getPointer() const {
11355
assert(isValid());
114-
return A.getPointer();
56+
return PointerAndKnownNonNull.getPointer();
11557
}
11658

11759
/// Return the type of the pointer value.
@@ -122,7 +64,7 @@ class Address {
12264
/// Return the type of the values stored in this address.
12365
llvm::Type *getElementType() const {
12466
assert(isValid());
125-
return A.getElementType();
67+
return ElementType;
12668
}
12769

12870
/// Return the address space that this address resides in.
@@ -138,7 +80,7 @@ class Address {
13880
/// Return the alignment of this pointer.
13981
CharUnits getAlignment() const {
14082
assert(isValid());
141-
return A.getAlignment();
83+
return Alignment;
14284
}
14385

14486
/// Return address with different pointer, but same element type and
@@ -159,13 +101,13 @@ class Address {
159101
/// Whether the pointer is known not to be null.
160102
KnownNonNull_t isKnownNonNull() const {
161103
assert(isValid());
162-
return A.isKnownNonNull();
104+
return (KnownNonNull_t)PointerAndKnownNonNull.getInt();
163105
}
164106

165107
/// Set the non-null bit.
166108
Address setKnownNonNull() {
167109
assert(isValid());
168-
A.setKnownNonNull();
110+
PointerAndKnownNonNull.setInt(true);
169111
return *this;
170112
}
171113
};

0 commit comments

Comments
 (0)