Skip to content

Commit acfd55b

Browse files
committed
Encode MCSymbol alignment as log2(align).
Given that alignments are always powers of 2, just encode it this way. This matches how we encode alignment on IR GlobalValue's for example. This compresses the CommonAlign member down to 5 bits which allows it to pack better with the surrounding fields. Reviewed by Duncan Exon Smith. llvm-svn: 241189
1 parent 3992e2c commit acfd55b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

llvm/include/llvm/MC/MCSymbol.h

+19-9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ class MCSymbol {
109109
/// extension and achieve better bitpacking with MSVC.
110110
unsigned SymbolContents : 2;
111111

112+
/// The alignment of the symbol, if it is 'common', or -1.
113+
///
114+
/// The alignment is stored as log2(align) + 1. This allows all values from
115+
/// 0 to 2^31 to be stored which is every power of 2 representable by an
116+
/// unsigned.
117+
static const unsigned NumCommonAlignmentBits = 5;
118+
unsigned CommonAlignLog2 : NumCommonAlignmentBits;
119+
112120
/// Index field, for use by the object file implementation.
113121
mutable uint32_t Index = 0;
114122

@@ -123,11 +131,6 @@ class MCSymbol {
123131
const MCExpr *Value;
124132
};
125133

126-
/// The alignment of the symbol, if it is 'common', or -1.
127-
//
128-
// FIXME: Pack this in with other fields?
129-
unsigned CommonAlign = -1U;
130-
131134
/// The Flags field is used by object file implementations to store
132135
/// additional per symbol information which is not easily classified.
133136
mutable uint32_t Flags = 0;
@@ -148,7 +151,8 @@ class MCSymbol {
148151
MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
149152
: IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
150153
IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
151-
Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset) {
154+
Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
155+
CommonAlignLog2(0) {
152156
Offset = 0;
153157
SectionOrFragmentAndHasName.setInt(!!Name);
154158
if (Name)
@@ -338,14 +342,20 @@ class MCSymbol {
338342
void setCommon(uint64_t Size, unsigned Align) {
339343
assert(getOffset() == 0);
340344
CommonSize = Size;
341-
CommonAlign = Align;
342345
SymbolContents = SymContentsCommon;
346+
347+
assert((!Align || isPowerOf2_32(Align)) &&
348+
"Alignment must be a power of 2");
349+
unsigned Log2Align = Log2_32(Align) + 1;
350+
assert(Log2Align < (1U << NumCommonAlignmentBits) &&
351+
"Out of range alignment");
352+
CommonAlignLog2 = Log2Align;
343353
}
344354

345355
/// Return the alignment of a 'common' symbol.
346356
unsigned getCommonAlignment() const {
347357
assert(isCommon() && "Not a 'common' symbol!");
348-
return CommonAlign;
358+
return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
349359
}
350360

351361
/// Declare this symbol as being 'common'.
@@ -356,7 +366,7 @@ class MCSymbol {
356366
bool declareCommon(uint64_t Size, unsigned Align) {
357367
assert(isCommon() || getOffset() == 0);
358368
if(isCommon()) {
359-
if(CommonSize != Size || CommonAlign != Align)
369+
if(CommonSize != Size || getCommonAlignment() != Align)
360370
return true;
361371
} else
362372
setCommon(Size, Align);

llvm/lib/MC/MCSymbol.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ using namespace llvm;
1919
// Sentinel value for the absolute pseudo section.
2020
MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
2121

22+
const unsigned MCSymbol::NumCommonAlignmentBits;
23+
2224
void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
2325
MCContext &Ctx) {
2426
// We may need more space for a Name to account for alignment. So allocate

0 commit comments

Comments
 (0)