LLVM: lib/CodeGen/BasicBlockSections.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
83#include
84
85using namespace llvm;
86
87
88
89
90
92 "bbsections-cold-text-prefix",
93 cl::desc("The text prefix to use for cold basic block clusters"),
95
97 "bbsections-detect-source-drift",
98 cl::desc("This checks if there is a fdo instr. profile hash "
99 "mismatch for this function"),
101
102namespace {
103
105public:
106 static char ID;
107
109
112 }
113
115 return "Basic Block Sections Analysis";
116 }
117
119
120
121
123
124private:
127};
128
129}
130
131char BasicBlockSections::ID = 0;
133 BasicBlockSections, "bbsections-prepare",
134 "Prepares for basic block sections, by splitting functions "
135 "into clusters of basic blocks.",
136 false, false)
140 "into clusters of basic blocks.",
142
143
144
145static void
150 for (auto &MBB : MF) {
152 auto *FTMBB = PreLayoutFallThroughs[MBB.getNumber()];
153
154
155
156
157
158
159 if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB))
161
162
163
165 continue;
166
167
168
169 Cond.clear();
172 continue;
174 }
175}
176
177
178
179
180
181
182
183
184
185
186
187static void
191
192
193
194 std::optional EHPadsSectionID;
195
196 for (auto &MBB : MF) {
197
198
199
200
202 FuncClusterInfo.empty()) {
203
204
205
206
208 } else {
210 if (I != FuncClusterInfo.end()) {
212 } else {
215
216 if (TII.isMBBSafeToSplitToCold(MBB)) {
217
218
220 }
221 }
222 }
223
226
227
228
231 }
232 }
233
234
235
237 for (auto &MBB : MF)
240}
241
246 for (auto &MBB : MF)
249
250 MF.sort(MBBCmp);
252 "Entry block should not be displaced by basic block sections");
253
254
256
257
258
259
261}
262
263
264
265
266
268 for (auto &MBB : MF) {
271 while (->isEHLabel())
272 ++MI;
274 }
275 }
276}
277
280 return false;
281
282 const char MetadataName[] = "instr_prof_hash_mismatch";
284 if (Existing) {
285 MDTuple *Tuple = cast(Existing);
286 for (const auto &N : Tuple->operands())
287 if (N.equalsStr(MetadataName))
288 return true;
289 }
290
291 return false;
292}
293
294
295
296bool BasicBlockSections::handleBBSections(MachineFunction &MF) {
298 if (BBSectionsType == BasicBlockSection::None)
299 return false;
300
301
302
303
304
305
306
307 if (BBSectionsType == BasicBlockSection::List &&
309 return false;
310
311
313
315 if (BBSectionsType == BasicBlockSection::List) {
316 auto [HasProfile, ClusterInfo] =
317 getAnalysis()
318 .getClusterInfoForFunction(MF.getName());
319 if (!HasProfile)
320 return false;
323 }
324 }
325
328
330 auto EntryBBSectionID = EntryBB.getSectionID();
331
332
333
334
335
336
337
338 auto MBBSectionOrder = [EntryBBSectionID](const MBBSectionID &LHS,
340
341
342 if (LHS == EntryBBSectionID || RHS == EntryBBSectionID)
343 return LHS == EntryBBSectionID;
344 return LHS.Type == RHS.Type ? LHS.Number < RHS.Number : LHS.Type < RHS.Type;
345 };
346
347
348
349
350
351
352
355 auto XSectionID = X.getSectionID();
356 auto YSectionID = Y.getSectionID();
357 if (XSectionID != YSectionID)
358 return MBBSectionOrder(XSectionID, YSectionID);
359
360 if (&X == &EntryBB || &Y == &EntryBB)
361 return &X == &EntryBB;
362
363
364 if (XSectionID.Type == MBBSectionID::SectionType::Default)
365 return FuncClusterInfo.lookup(*X.getBBID()).PositionInCluster <
366 FuncClusterInfo.lookup(*Y.getBBID()).PositionInCluster;
367 return X.getNumber() < Y.getNumber();
368 };
369
372 return true;
373}
374
375
376
377
378
379bool BasicBlockSections::handleBBAddrMap(MachineFunction &MF) {
381 return false;
383 return true;
384}
385
386bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
387
388 auto R1 = handleBBSections(MF);
389
390 auto R2 = handleBBAddrMap(MF);
391
392
393 if (auto *WP = getAnalysisIfAvailable())
394 WP->getDomTree().updateBlockNumbers();
395 if (auto *WP = getAnalysisIfAvailable())
396 WP->getPostDomTree().updateBlockNumbers();
397
398 return R1 || R2;
399}
400
401void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const {
407}
408
410 return new BasicBlockSections();
411}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
Lower uses of LDS variables from non kernel functions
static void assignSections(MachineFunction &MF, const DenseMap< UniqueBBID, BBClusterInfo > &FuncClusterInfo)
static cl::opt< bool > BBSectionsDetectSourceDrift("bbsections-detect-source-drift", cl::desc("This checks if there is a fdo instr. profile hash " "mismatch for this function"), cl::init(true), cl::Hidden)
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
bbsections Prepares for basic block by splitting functions into clusters of basic static false void updateBranches(MachineFunction &MF, const SmallVector< MachineBasicBlock * > &PreLayoutFallThroughs)
bbsections Prepares for basic block sections
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const HexagonInstrInfo * TII
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
unify loop Fixup each natural loop to have a single exit block
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
ArrayRef< MDOperand > operands() const
bool isEHPad() const
Returns true if the block is a landing pad.
MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
std::optional< UniqueBBID > getBBID() const
void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor)
Update the terminator instructions in block to account for changes to block layout which may have bee...
MBBSectionID getSectionID() const
Returns the section ID of this basic block.
void setSectionID(MBBSectionID V)
Sets the section ID for this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool isBeginSection() const
Returns true if this block begins any section.
DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
bool isEndSection() const
Returns true if this block ends any section.
Analysis pass which computes a MachineDominatorTree.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void setBBSectionsType(BasicBlockSection V)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool hasBBSections() const
Returns true if this function has basic block sections enabled.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
void assignBeginEndSections()
Assign IsBeginSection IsEndSection fields for basic blocks in this function.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
virtual void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Insert a noop into the instruction stream at the specified point.
llvm::BasicBlockSection getBBSectionsType() const
If basic blocks should be emitted into their own section, corresponding to -fbasic-block-sections.
virtual const TargetInstrInfo * getInstrInfo() const
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineFunctionPass * createBasicBlockSectionsPass()
createBasicBlockSections Pass - This pass assigns sections to machine basic blocks and is enabled wit...
bool hasInstrProfHashMismatch(MachineFunction &MF)
This checks if the source of this function has drifted since this binary was profiled previously.
void initializeBasicBlockSectionsPass(PassRegistry &)
void avoidZeroOffsetLandingPad(MachineFunction &MF)
cl::opt< std::string > BBSectionsColdTextPrefix
void sortBasicBlocksAndUpdateBranches(MachineFunction &MF, MachineBasicBlockComparator MBBCmp)
static const MBBSectionID ExceptionSectionID
static const MBBSectionID ColdSectionID