Skip to content

Commit 4ddf7ab

Browse files
committed
Revert "Extend BasicBlock sections to allow specifying clusters of basic blocks"
This reverts commit 0d4ec16 Because tests were not added to the commit.
1 parent 5c7bbe3 commit 4ddf7ab

22 files changed

+409
-492
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ class AsmPrinter : public MachineFunctionPass {
141141
MCSymbol *CurrentFnEnd = nullptr;
142142
MCSymbol *CurExceptionSym = nullptr;
143143

144-
// The symbol used to represent the start of the current BB section of the
145-
// function. This is used to calculate the size of the BB section.
146-
MCSymbol *CurrentSectionBeginSym = nullptr;
147-
148144
// The garbage collection metadata printer table.
149145
void *GCMetadataPrinters = nullptr; // Really a DenseMap.
150146

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,17 @@ class raw_ostream;
4646
class TargetRegisterClass;
4747
class TargetRegisterInfo;
4848

49-
// This structure uniquely identifies a basic block section.
50-
// Possible values are
51-
// {Type: Default, Number: (unsigned)} (These are regular section IDs)
52-
// {Type: Exception, Number: 0} (ExceptionSectionID)
53-
// {Type: Cold, Number: 0} (ColdSectionID)
54-
struct MBBSectionID {
55-
enum SectionType {
56-
Default = 0, // Regular section (these sections are distinguished by the
57-
// Number field).
58-
Exception, // Special section type for exception handling blocks
59-
Cold, // Special section type for cold blocks
60-
} Type;
61-
unsigned Number;
62-
63-
MBBSectionID(unsigned N) : Type(Default), Number(N) {}
64-
65-
// Special unique sections for cold and exception blocks.
66-
const static MBBSectionID ColdSectionID;
67-
const static MBBSectionID ExceptionSectionID;
68-
69-
bool operator==(const MBBSectionID &Other) const {
70-
return Type == Other.Type && Number == Other.Number;
71-
}
72-
73-
bool operator!=(const MBBSectionID &Other) const { return !(*this == Other); }
74-
75-
private:
76-
// This is only used to construct the special cold and exception sections.
77-
MBBSectionID(SectionType T) : Type(T), Number(0) {}
49+
enum MachineBasicBlockSection : unsigned {
50+
/// This is also the order of sections in a function. Basic blocks that are
51+
/// part of the original function section (entry block) come first, followed
52+
/// by exception handling basic blocks, cold basic blocks and finally basic
53+
// blocks that need unique sections.
54+
MBBS_Entry,
55+
MBBS_Exception,
56+
MBBS_Cold,
57+
MBBS_Unique,
58+
/// None implies no sections for any basic block, the default.
59+
MBBS_None,
7860
};
7961

8062
template <> struct ilist_traits<MachineInstr> {
@@ -161,14 +143,8 @@ class MachineBasicBlock
161143
/// Indicate that this basic block is the entry block of a cleanup funclet.
162144
bool IsCleanupFuncletEntry = false;
163145

164-
/// With basic block sections, this stores the Section ID of the basic block.
165-
MBBSectionID SectionID{0};
166-
167-
// Indicate that this basic block begins a section.
168-
bool IsBeginSection = false;
169-
170-
// Indicate that this basic block ends a section.
171-
bool IsEndSection = false;
146+
/// Stores the Section type of the basic block with basic block sections.
147+
MachineBasicBlockSection SectionType = MBBS_None;
172148

173149
/// Default target of the callbr of a basic block.
174150
bool InlineAsmBrDefaultTarget = false;
@@ -459,20 +435,16 @@ class MachineBasicBlock
459435
void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
460436

461437
/// Returns true if this block begins any section.
462-
bool isBeginSection() const { return IsBeginSection; }
438+
bool isBeginSection() const;
463439

464440
/// Returns true if this block ends any section.
465-
bool isEndSection() const { return IsEndSection; }
441+
bool isEndSection() const;
466442

467-
void setIsBeginSection(bool V = true) { IsBeginSection = V; }
443+
/// Returns the type of section this basic block belongs to.
444+
MachineBasicBlockSection getSectionType() const { return SectionType; }
468445

469-
void setIsEndSection(bool V = true) { IsEndSection = V; }
470-
471-
/// Returns the section ID of this basic block.
472-
MBBSectionID getSectionID() const { return SectionID; }
473-
474-
/// Sets the section ID for this basic block.
475-
void setSectionID(MBBSectionID V) { SectionID = V; }
446+
/// Indicate that the basic block belongs to a Section Type.
447+
void setSectionType(MachineBasicBlockSection V) { SectionType = V; }
476448

477449
/// Returns true if this is the indirect dest of an INLINEASM_BR.
478450
bool isInlineAsmBrIndirectTarget(const MachineBasicBlock *Tgt) const {
@@ -513,9 +485,10 @@ class MachineBasicBlock
513485
void moveAfter(MachineBasicBlock *NewBefore);
514486

515487
/// Returns true if this and MBB belong to the same section.
516-
bool sameSection(const MachineBasicBlock *MBB) const {
517-
return getSectionID() == MBB->getSectionID();
518-
}
488+
bool sameSection(const MachineBasicBlock *MBB) const;
489+
490+
/// Returns the basic block that ends the section which contains this one.
491+
const MachineBasicBlock *getSectionEndMBB() const;
519492

520493
/// Update the terminator instructions in block to account for changes to the
521494
/// layout. If the block previously used a fallthrough, it may now need a
@@ -903,6 +876,12 @@ class MachineBasicBlock
903876
/// Return the MCSymbol for this basic block.
904877
MCSymbol *getSymbol() const;
905878

879+
/// Sets the MCSymbol corresponding to the end of this basic block.
880+
void setEndMCSymbol(MCSymbol *Sym) { EndMCSymbol = Sym; }
881+
882+
/// Returns the MCSymbol corresponding to the end of this basic block.
883+
MCSymbol *getEndMCSymbol() const { return EndMCSymbol; }
884+
906885
Optional<uint64_t> getIrrLoopHeaderWeight() const {
907886
return IrrLoopHeaderWeight;
908887
}

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ class MachineFunction {
346346
/// Section Type for basic blocks, only relevant with basic block sections.
347347
BasicBlockSection BBSectionsType = BasicBlockSection::None;
348348

349+
/// With Basic Block Sections, this stores the bb ranges of cold and
350+
/// exception sections.
351+
std::pair<int, int> ColdSectionRange = {-1, -1};
352+
std::pair<int, int> ExceptionSectionRange = {-1, -1};
353+
349354
/// List of C++ TypeInfo used.
350355
std::vector<const GlobalValue *> TypeInfos;
351356

@@ -503,13 +508,22 @@ class MachineFunction {
503508

504509
void setBBSectionsType(BasicBlockSection V) { BBSectionsType = V; }
505510

511+
void setSectionRange();
512+
513+
/// Returns true if this basic block number starts a cold or exception
514+
/// section.
515+
bool isSectionStartMBB(int N) const {
516+
return (N == ColdSectionRange.first || N == ExceptionSectionRange.first);
517+
}
518+
519+
/// Returns true if this basic block ends a cold or exception section.
520+
bool isSectionEndMBB(int N) const {
521+
return (N == ColdSectionRange.second || N == ExceptionSectionRange.second);
522+
}
523+
506524
/// Creates basic block Labels for this function.
507525
void createBBLabels();
508526

509-
/// Assign IsBeginSection IsEndSection fields for basic blocks in this
510-
/// function.
511-
void assignBeginEndSections();
512-
513527
/// getTarget - Return the target machine this machine code is compiled with
514528
const LLVMTargetMachine &getTarget() const { return Target; }
515529

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
6969
const MachineBasicBlock &MBB,
7070
const TargetMachine &TM) const override;
7171

72+
MCSection *getNamedSectionForMachineBasicBlock(
73+
const Function &F, const MachineBasicBlock &MBB, const TargetMachine &TM,
74+
const char *Suffix) const override;
75+
7276
bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
7377
const Function &F) const override;
7478

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
9292
const MachineBasicBlock &MBB,
9393
const TargetMachine &TM) const;
9494

95+
virtual MCSection *getNamedSectionForMachineBasicBlock(
96+
const Function &F, const MachineBasicBlock &MBB, const TargetMachine &TM,
97+
const char *Suffix) const;
98+
9599
/// Classify the specified global variable into a set of target independent
96100
/// categories embodied in SectionKind.
97101
static SectionKind getKindForGlobal(const GlobalObject *GO,

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,14 @@ void AsmPrinter::emitFunctionBody() {
10971097
// Print out code for the function.
10981098
bool HasAnyRealCode = false;
10991099
int NumInstsInFunction = 0;
1100+
bool emitBBSections = MF->hasBBSections();
1101+
MachineBasicBlock *EndOfRegularSectionMBB = nullptr;
1102+
if (emitBBSections) {
1103+
EndOfRegularSectionMBB =
1104+
const_cast<MachineBasicBlock *>(MF->front().getSectionEndMBB());
1105+
assert(EndOfRegularSectionMBB->isEndSection() &&
1106+
"The MBB at the end of the regular section must end a section");
1107+
}
11001108

11011109
for (auto &MBB : *MF) {
11021110
// Print a label for the basic block.
@@ -1177,41 +1185,17 @@ void AsmPrinter::emitFunctionBody() {
11771185
}
11781186
}
11791187
}
1180-
1181-
// We need a temporary symbol for the end of this basic block, if either we
1182-
// have BBLabels enabled and we want to emit size directive for the BBs, or
1183-
// if this basic blocks marks the end of a section (except the section
1184-
// containing the entry basic block as the end symbol for that section is
1185-
// CurrentFnEnd).
1186-
MCSymbol *CurrentBBEnd = nullptr;
1187-
if ((MAI->hasDotTypeDotSizeDirective() && MF->hasBBLabels()) ||
1188-
(MBB.isEndSection() && !MBB.sameSection(&MF->front()))) {
1189-
CurrentBBEnd = OutContext.createTempSymbol();
1190-
OutStreamer->emitLabel(CurrentBBEnd);
1191-
}
1192-
1193-
// Helper for emitting the size directive associated with a basic block
1194-
// symbol.
1195-
auto emitELFSizeDirective = [&](MCSymbol *SymForSize) {
1196-
assert(CurrentBBEnd && "Basicblock end symbol not set!");
1188+
if (&MBB != EndOfRegularSectionMBB &&
1189+
(MF->hasBBLabels() || MBB.isEndSection())) {
1190+
// Emit size directive for the size of this basic block. Create a symbol
1191+
// for the end of the basic block.
1192+
MCSymbol *CurrentBBEnd = OutContext.createTempSymbol();
11971193
const MCExpr *SizeExp = MCBinaryExpr::createSub(
11981194
MCSymbolRefExpr::create(CurrentBBEnd, OutContext),
1199-
MCSymbolRefExpr::create(SymForSize, OutContext), OutContext);
1200-
OutStreamer->emitELFSize(SymForSize, SizeExp);
1201-
};
1202-
1203-
// Emit size directive for the size of each basic block, if BBLabels is
1204-
// enabled.
1205-
if (MAI->hasDotTypeDotSizeDirective() && MF->hasBBLabels())
1206-
emitELFSizeDirective(MBB.getSymbol());
1207-
1208-
// Emit size directive for the size of each basic block section once we
1209-
// get to the end of that section.
1210-
if (MBB.isEndSection()) {
1211-
if (!MBB.sameSection(&MF->front())) {
1212-
if (MAI->hasDotTypeDotSizeDirective())
1213-
emitELFSizeDirective(CurrentSectionBeginSym);
1214-
}
1195+
MCSymbolRefExpr::create(MBB.getSymbol(), OutContext), OutContext);
1196+
OutStreamer->emitLabel(CurrentBBEnd);
1197+
MBB.setEndMCSymbol(CurrentBBEnd);
1198+
OutStreamer->emitELFSize(MBB.getSymbol(), SizeExp);
12151199
}
12161200
emitBasicBlockEnd(MBB);
12171201
}
@@ -1246,8 +1230,9 @@ void AsmPrinter::emitFunctionBody() {
12461230
}
12471231
}
12481232

1249-
// Switch to the original section in case basic block sections was used.
1250-
OutStreamer->SwitchSection(MF->getSection());
1233+
// Switch to the original section if basic block sections was used.
1234+
if (emitBBSections)
1235+
OutStreamer->SwitchSection(MF->getSection());
12511236

12521237
const Function &F = MF->getFunction();
12531238
for (const auto &BB : F) {
@@ -1264,7 +1249,7 @@ void AsmPrinter::emitFunctionBody() {
12641249
emitFunctionBodyEnd();
12651250

12661251
if (needFuncLabelsForEHOrDebugInfo(*MF, MMI) ||
1267-
MAI->hasDotTypeDotSizeDirective()) {
1252+
MAI->hasDotTypeDotSizeDirective() || emitBBSections) {
12681253
// Create a symbol for the end of function.
12691254
CurrentFnEnd = createTempSymbol("func_end");
12701255
OutStreamer->emitLabel(CurrentFnEnd);
@@ -1287,6 +1272,8 @@ void AsmPrinter::emitFunctionBody() {
12871272
HI.Handler->markFunctionEnd();
12881273
}
12891274

1275+
if (emitBBSections)
1276+
EndOfRegularSectionMBB->setEndMCSymbol(CurrentFnEnd);
12901277

12911278
// Print out jump tables referenced by the function.
12921279
emitJumpTableInfo();
@@ -1766,7 +1753,6 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
17661753

17671754
CurrentFnSymForSize = CurrentFnSym;
17681755
CurrentFnBegin = nullptr;
1769-
CurrentSectionBeginSym = nullptr;
17701756
CurExceptionSym = nullptr;
17711757
bool NeedsLocalForSize = MAI->needsLocalForSize();
17721758
if (F.hasFnAttribute("patchable-function-entry") ||
@@ -2995,6 +2981,7 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
29952981
/// MachineBasicBlock, an alignment (if present) and a comment describing
29962982
/// it if appropriate.
29972983
void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
2984+
bool BBSections = MF->hasBBSections();
29982985
// End the previous funclet and start a new one.
29992986
if (MBB.isEHFuncletEntry()) {
30002987
for (const HandlerInfo &HI : Handlers) {
@@ -3004,9 +2991,11 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
30042991
}
30052992

30062993
// Emit an alignment directive for this block, if needed.
3007-
const Align Alignment = MBB.getAlignment();
3008-
if (Alignment != Align(1))
3009-
emitAlignment(Alignment);
2994+
if (MBB.pred_empty() || !BBSections) {
2995+
const Align Alignment = MBB.getAlignment();
2996+
if (Alignment != Align(1))
2997+
emitAlignment(Alignment);
2998+
}
30102999

30113000
// If the block has its address taken, emit any labels that were used to
30123001
// reference the block. It is possible that there is more than one label
@@ -3038,8 +3027,9 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
30383027
emitBasicBlockLoopComments(MBB, MLI, *this);
30393028
}
30403029

3030+
bool emitBBLabels = BBSections || MF->hasBBLabels();
30413031
if (MBB.pred_empty() ||
3042-
(!MF->hasBBLabels() && isBlockOnlyReachableByFallthrough(&MBB) &&
3032+
(!emitBBLabels && isBlockOnlyReachableByFallthrough(&MBB) &&
30433033
!MBB.isEHFuncletEntry() && !MBB.hasLabelMustBeEmitted())) {
30443034
if (isVerbose()) {
30453035
// NOTE: Want this comment at start of line, don't emit with AddComment.
@@ -3050,12 +3040,23 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
30503040
if (isVerbose() && MBB.hasLabelMustBeEmitted()) {
30513041
OutStreamer->AddComment("Label of block must be emitted");
30523042
}
3053-
// Switch to a new section if this basic block must begin a section.
3054-
if (MBB.isBeginSection()) {
3043+
// With -fbasicblock-sections, a basic block can start a new section.
3044+
if (MBB.getSectionType() == MachineBasicBlockSection::MBBS_Exception) {
3045+
// Create the exception section for this function.
3046+
OutStreamer->SwitchSection(
3047+
getObjFileLowering().getNamedSectionForMachineBasicBlock(
3048+
MF->getFunction(), MBB, TM, ".eh"));
3049+
} else if (MBB.getSectionType() == MachineBasicBlockSection::MBBS_Cold) {
3050+
// Create the cold section here.
3051+
OutStreamer->SwitchSection(
3052+
getObjFileLowering().getNamedSectionForMachineBasicBlock(
3053+
MF->getFunction(), MBB, TM, ".unlikely"));
3054+
} else if (MBB.isBeginSection() && MBB.isEndSection()) {
30553055
OutStreamer->SwitchSection(
30563056
getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(),
30573057
MBB, TM));
3058-
CurrentSectionBeginSym = MBB.getSymbol();
3058+
} else if (BBSections) {
3059+
OutStreamer->SwitchSection(MF->getSection());
30593060
}
30603061
OutStreamer->emitLabel(MBB.getSymbol());
30613062
}
@@ -3089,7 +3090,7 @@ void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility,
30893090
/// the predecessor and this block is a fall-through.
30903091
bool AsmPrinter::
30913092
isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
3092-
// With BasicBlock Sections, beginning of the section is not a fallthrough.
3093+
// With BasicBlock Sections, no block is a fall through.
30933094
if (MBB->isBeginSection())
30943095
return false;
30953096

0 commit comments

Comments
 (0)