Skip to content

Commit 531de38

Browse files
aengelkeAlexisPerry
authored andcommitted
[MC] Make MCSection::isVirtualSection non-virtual (llvm#96920)
This method is called once per encoded instruction, but never changes throughout the lifetime of a section. Store this information as a bit flag in the MCSection instead.
1 parent 7d28155 commit 531de38

15 files changed

+32
-54
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class MCSection {
106106

107107
bool IsText : 1;
108108

109+
bool IsVirtual : 1;
110+
109111
MCDummyFragment DummyFragment;
110112

111113
// Mapping from subsection number to fragment list. At layout time, the
@@ -127,7 +129,8 @@ class MCSection {
127129
StringRef Name;
128130
SectionVariant Variant;
129131

130-
MCSection(SectionVariant V, StringRef Name, bool IsText, MCSymbol *Begin);
132+
MCSection(SectionVariant V, StringRef Name, bool IsText, bool IsVirtual,
133+
MCSymbol *Begin);
131134
~MCSection();
132135

133136
public:
@@ -219,7 +222,7 @@ class MCSection {
219222

220223
/// Check whether this section is "virtual", that is has no actual object
221224
/// file contents.
222-
virtual bool isVirtualSection() const = 0;
225+
bool isVirtualSection() const { return IsVirtual; }
223226

224227
virtual StringRef getVirtualSectionKind() const;
225228

llvm/include/llvm/MC/MCSectionCOFF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MCSectionCOFF final : public MCSection {
5353
MCSectionCOFF(StringRef Name, unsigned Characteristics,
5454
MCSymbol *COMDATSymbol, int Selection, MCSymbol *Begin)
5555
: MCSection(SV_COFF, Name, Characteristics & COFF::IMAGE_SCN_CNT_CODE,
56+
Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA,
5657
Begin),
5758
Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
5859
Selection(Selection) {
@@ -75,7 +76,6 @@ class MCSectionCOFF final : public MCSection {
7576
raw_ostream &OS,
7677
uint32_t Subsection) const override;
7778
bool useCodeAlign() const override;
78-
bool isVirtualSection() const override;
7979
StringRef getVirtualSectionKind() const override;
8080

8181
unsigned getOrAssignWinCFISectionID(unsigned *NextID) const {

llvm/include/llvm/MC/MCSectionDXContainer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class MCSectionDXContainer final : public MCSection {
2424
friend class MCContext;
2525

2626
MCSectionDXContainer(StringRef Name, SectionKind K, MCSymbol *Begin)
27-
: MCSection(SV_DXContainer, Name, K.isText(), Begin) {}
27+
: MCSection(SV_DXContainer, Name, K.isText(), /*IsVirtual=*/false,
28+
Begin) {}
2829

2930
public:
3031
void printSwitchToSection(const MCAsmInfo &, const Triple &, raw_ostream &,
3132
uint32_t) const override;
3233
bool useCodeAlign() const override { return false; }
33-
bool isVirtualSection() const override { return false; }
3434
};
3535

3636
} // end namespace llvm

llvm/include/llvm/MC/MCSectionELF.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class MCSectionELF final : public MCSection {
5454
unsigned entrySize, const MCSymbolELF *group, bool IsComdat,
5555
unsigned UniqueID, MCSymbol *Begin,
5656
const MCSymbolELF *LinkedToSym)
57-
: MCSection(SV_ELF, Name, flags & ELF::SHF_EXECINSTR, Begin), Type(type),
58-
Flags(flags), UniqueID(UniqueID), EntrySize(entrySize),
57+
: MCSection(SV_ELF, Name, flags & ELF::SHF_EXECINSTR,
58+
type == ELF::SHT_NOBITS, Begin),
59+
Type(type), Flags(flags), UniqueID(UniqueID), EntrySize(entrySize),
5960
Group(group, IsComdat), LinkedToSym(LinkedToSym) {
6061
if (Group.getPointer())
6162
Group.getPointer()->setIsSignature();
@@ -81,7 +82,6 @@ class MCSectionELF final : public MCSection {
8182
raw_ostream &OS,
8283
uint32_t Subsection) const override;
8384
bool useCodeAlign() const override;
84-
bool isVirtualSection() const override;
8585
StringRef getVirtualSectionKind() const override;
8686

8787
bool isUnique() const { return UniqueID != NonUniqueID; }

llvm/include/llvm/MC/MCSectionGOFF.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class MCSectionGOFF final : public MCSection {
3030

3131
friend class MCContext;
3232
MCSectionGOFF(StringRef Name, SectionKind K, MCSection *P, uint32_t Sub)
33-
: MCSection(SV_GOFF, Name, K.isText(), nullptr), Parent(P),
34-
Subsection(Sub) {}
33+
: MCSection(SV_GOFF, Name, K.isText(), /*IsVirtual=*/false, nullptr),
34+
Parent(P), Subsection(Sub) {}
3535

3636
public:
3737
void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
@@ -42,8 +42,6 @@ class MCSectionGOFF final : public MCSection {
4242

4343
bool useCodeAlign() const override { return false; }
4444

45-
bool isVirtualSection() const override { return false; }
46-
4745
MCSection *getParent() const { return Parent; }
4846
uint32_t getSubsection() const { return Subsection; }
4947

llvm/include/llvm/MC/MCSectionMachO.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class MCSectionMachO final : public MCSection {
7575
raw_ostream &OS,
7676
uint32_t Subsection) const override;
7777
bool useCodeAlign() const override;
78-
bool isVirtualSection() const override;
7978

8079
void allocAtoms();
8180
const MCSymbol *getAtom(size_t I) const;

llvm/include/llvm/MC/MCSectionSPIRV.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class MCSectionSPIRV final : public MCSection {
2424
friend class MCContext;
2525

2626
MCSectionSPIRV()
27-
: MCSection(SV_SPIRV, "", /*IsText=*/true, /*Begin=*/nullptr) {}
27+
: MCSection(SV_SPIRV, "", /*IsText=*/true, /*IsVirtual=*/false,
28+
/*Begin=*/nullptr) {}
2829
// TODO: Add StringRef Name to MCSectionSPIRV.
2930

3031
public:
@@ -33,7 +34,6 @@ class MCSectionSPIRV final : public MCSection {
3334
raw_ostream &OS,
3435
uint32_t Subsection) const override {}
3536
bool useCodeAlign() const override { return false; }
36-
bool isVirtualSection() const override { return false; }
3737
};
3838

3939
} // end namespace llvm

llvm/include/llvm/MC/MCSectionWasm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class MCSectionWasm final : public MCSection {
5151
friend class MCContext;
5252
MCSectionWasm(StringRef Name, SectionKind K, unsigned SegmentFlags,
5353
const MCSymbolWasm *Group, unsigned UniqueID, MCSymbol *Begin)
54-
: MCSection(SV_Wasm, Name, K.isText(), Begin), UniqueID(UniqueID),
55-
Group(Group), IsWasmData(K.isReadOnly() || K.isWriteable()),
54+
: MCSection(SV_Wasm, Name, K.isText(), /*IsVirtual=*/false, Begin),
55+
UniqueID(UniqueID), Group(Group),
56+
IsWasmData(K.isReadOnly() || K.isWriteable()),
5657
IsMetadata(K.isMetadata()), SegmentFlags(SegmentFlags) {}
5758

5859
public:
@@ -67,7 +68,6 @@ class MCSectionWasm final : public MCSection {
6768
raw_ostream &OS,
6869
uint32_t Subsection) const override;
6970
bool useCodeAlign() const override;
70-
bool isVirtualSection() const override;
7171

7272
bool isWasmData() const { return IsWasmData; }
7373
bool isMetadata() const { return IsMetadata; }

llvm/include/llvm/MC/MCSectionXCOFF.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ class MCSectionXCOFF final : public MCSection {
4141
static constexpr unsigned DefaultAlignVal = 4;
4242
static constexpr unsigned DefaultTextAlignVal = 32;
4343

44+
// XTY_CM sections are virtual except for toc-data symbols.
4445
MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC,
4546
XCOFF::SymbolType ST, SectionKind K, MCSymbolXCOFF *QualName,
4647
MCSymbol *Begin, StringRef SymbolTableName,
4748
bool MultiSymbolsAllowed)
48-
: MCSection(SV_XCOFF, Name, K.isText(), Begin),
49+
: MCSection(SV_XCOFF, Name, K.isText(),
50+
/*IsVirtual=*/ST == XCOFF::XTY_CM && SMC != XCOFF::XMC_TD,
51+
Begin),
4952
CsectProp(XCOFF::CsectProperties(SMC, ST)), QualName(QualName),
5053
SymbolTableName(SymbolTableName), DwarfSubtypeFlags(std::nullopt),
5154
MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
@@ -69,12 +72,14 @@ class MCSectionXCOFF final : public MCSection {
6972
}
7073
}
7174

75+
// DWARF sections are never virtual.
7276
MCSectionXCOFF(StringRef Name, SectionKind K, MCSymbolXCOFF *QualName,
7377
XCOFF::DwarfSectionSubtypeFlags DwarfSubtypeFlags,
7478
MCSymbol *Begin, StringRef SymbolTableName,
7579
bool MultiSymbolsAllowed)
76-
: MCSection(SV_XCOFF, Name, K.isText(), Begin), QualName(QualName),
77-
SymbolTableName(SymbolTableName), DwarfSubtypeFlags(DwarfSubtypeFlags),
80+
: MCSection(SV_XCOFF, Name, K.isText(), /*IsVirtual=*/false, Begin),
81+
QualName(QualName), SymbolTableName(SymbolTableName),
82+
DwarfSubtypeFlags(DwarfSubtypeFlags),
7883
MultiSymbolsAllowed(MultiSymbolsAllowed), Kind(K) {
7984
assert(QualName != nullptr && "QualName is needed.");
8085

@@ -114,7 +119,6 @@ class MCSectionXCOFF final : public MCSection {
114119
raw_ostream &OS,
115120
uint32_t Subsection) const override;
116121
bool useCodeAlign() const override;
117-
bool isVirtualSection() const override;
118122
StringRef getSymbolTableName() const { return SymbolTableName; }
119123
void setSymbolTableName(StringRef STN) { SymbolTableName = STN; }
120124
bool isMultiSymbolsAllowed() const { return MultiSymbolsAllowed; }

llvm/lib/MC/MCSection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
using namespace llvm;
2222

2323
MCSection::MCSection(SectionVariant V, StringRef Name, bool IsText,
24-
MCSymbol *Begin)
24+
bool IsVirtual, MCSymbol *Begin)
2525
: Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
26-
HasLayout(false), IsRegistered(false), IsText(IsText), Name(Name),
27-
Variant(V) {
26+
HasLayout(false), IsRegistered(false), IsText(IsText),
27+
IsVirtual(IsVirtual), Name(Name), Variant(V) {
2828
DummyFragment.setParent(this);
2929
// The initial subsection number is 0. Create a fragment list.
3030
CurFragList = &Subsections.emplace_back(0u, FragList{}).second;

llvm/lib/MC/MCSectionCOFF.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
108108

109109
bool MCSectionCOFF::useCodeAlign() const { return isText(); }
110110

111-
bool MCSectionCOFF::isVirtualSection() const {
112-
return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
113-
}
114-
115111
StringRef MCSectionCOFF::getVirtualSectionKind() const {
116112
return "IMAGE_SCN_CNT_UNINITIALIZED_DATA";
117113
}

llvm/lib/MC/MCSectionELF.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,4 @@ bool MCSectionELF::useCodeAlign() const {
210210
return getFlags() & ELF::SHF_EXECINSTR;
211211
}
212212

213-
bool MCSectionELF::isVirtualSection() const {
214-
return getType() == ELF::SHT_NOBITS;
215-
}
216-
217213
StringRef MCSectionELF::getVirtualSectionKind() const { return "SHT_NOBITS"; }

llvm/lib/MC/MCSectionMachO.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
9292
MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
9393
unsigned TAA, unsigned reserved2, SectionKind K,
9494
MCSymbol *Begin)
95-
: MCSection(SV_MachO, Section, K.isText(), Begin), TypeAndAttributes(TAA),
96-
Reserved2(reserved2) {
95+
: MCSection(SV_MachO, Section, K.isText(),
96+
MachO::isVirtualSection(TAA & MachO::SECTION_TYPE), Begin),
97+
TypeAndAttributes(TAA), Reserved2(reserved2) {
9798
assert(Segment.size() <= 16 && Section.size() <= 16 &&
9899
"Segment or section string too long");
99100
for (unsigned i = 0; i != 16; ++i) {
@@ -172,12 +173,6 @@ bool MCSectionMachO::useCodeAlign() const {
172173
return hasAttribute(MachO::S_ATTR_PURE_INSTRUCTIONS);
173174
}
174175

175-
bool MCSectionMachO::isVirtualSection() const {
176-
return (getType() == MachO::S_ZEROFILL ||
177-
getType() == MachO::S_GB_ZEROFILL ||
178-
getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
179-
}
180-
181176
/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
182177
/// This is a string that can appear after a .section directive in a mach-o
183178
/// flavored .s file. If successful, this fills in the specified Out

llvm/lib/MC/MCSectionWasm.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,3 @@ void MCSectionWasm::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
9999
}
100100

101101
bool MCSectionWasm::useCodeAlign() const { return false; }
102-
103-
bool MCSectionWasm::isVirtualSection() const { return false; }

llvm/lib/MC/MCSectionXCOFF.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,3 @@ void MCSectionXCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
132132
}
133133

134134
bool MCSectionXCOFF::useCodeAlign() const { return getKind().isText(); }
135-
136-
bool MCSectionXCOFF::isVirtualSection() const {
137-
// DWARF sections are always not virtual.
138-
if (isDwarfSect())
139-
return false;
140-
assert(isCsect() &&
141-
"Handling for isVirtualSection not implemented for this section!");
142-
// XTY_CM sections are virtual except for toc-data symbols.
143-
return (XCOFF::XTY_CM == CsectProp->Type) &&
144-
(getMappingClass() != XCOFF::XMC_TD);
145-
}

0 commit comments

Comments
 (0)