Skip to content

Commit 6b9ac2a

Browse files
authored
[BranchFolding] Add a hook to override tail merge size (#99025)
A new hook `TargetInstrInfo::getTailMergeSize()` is added so that targets can override it. This removes an existing TODO.
1 parent 8dafbb5 commit 6b9ac2a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,12 @@ class TargetInstrInfo : public MCInstrInfo {
22262226
return OptLevel >= CodeGenOptLevel::Aggressive ? 4 : 2;
22272227
}
22282228

2229+
/// Returns the target-specific default value for tail merging.
2230+
/// This value will be used if the tail-merge-size argument is not provided.
2231+
virtual unsigned getTailMergeSize(const MachineFunction &MF) const {
2232+
return 3;
2233+
}
2234+
22292235
/// Returns the callee operand from the given \p MI.
22302236
virtual const MachineOperand &getCalleeOperand(const MachineInstr &MI) const {
22312237
return MI.getOperand(0);

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ TailMergeThreshold("tail-merge-threshold",
8080
cl::init(150), cl::Hidden);
8181

8282
// Heuristic for tail merging (and, inversely, tail duplication).
83-
// TODO: This should be replaced with a target query.
8483
static cl::opt<unsigned>
8584
TailMergeSize("tail-merge-size",
8685
cl::desc("Min number of instructions to consider tail merging"),
@@ -145,8 +144,6 @@ BranchFolder::BranchFolder(bool DefaultEnableTailMerge, bool CommonHoist,
145144
ProfileSummaryInfo *PSI, unsigned MinTailLength)
146145
: EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
147146
MBBFreqInfo(FreqInfo), MBPI(ProbInfo), PSI(PSI) {
148-
if (MinCommonTailLength == 0)
149-
MinCommonTailLength = TailMergeSize;
150147
switch (FlagEnableTailMerge) {
151148
case cl::BOU_UNSET:
152149
EnableTailMerge = DefaultEnableTailMerge;
@@ -195,6 +192,12 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF,
195192
MLI = mli;
196193
this->MRI = &MRI;
197194

195+
if (MinCommonTailLength == 0) {
196+
MinCommonTailLength = TailMergeSize.getNumOccurrences() > 0
197+
? TailMergeSize
198+
: TII->getTailMergeSize(MF);
199+
}
200+
198201
UpdateLiveIns = MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF);
199202
if (!UpdateLiveIns)
200203
MRI.invalidateLiveness();

0 commit comments

Comments
 (0)