[llvm] [EarlyIfCvt][NFC] Factor out isProfitableToConvertIf (PR #98011)
Pengcheng Wang via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 8 04:20:08 PDT 2024
https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/98011
This function is used to decide if the conversion from branch to
select is profitable.
Currently, only branch probabilities heuristic is applied.
This is used in #97808.
>From a1137eae8bd9e69d5830a0b13e67fed9a85172fd Mon Sep 17 00:00:00 2001
From: Wang Pengcheng <wangpengcheng.pp at bytedance.com>
Date: Mon, 8 Jul 2024 19:19:57 +0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.6-beta.1
---
llvm/lib/CodeGen/EarlyIfConversion.cpp | 84 +++++++++++++++-----------
1 file changed, 48 insertions(+), 36 deletions(-)
diff --git a/llvm/lib/CodeGen/EarlyIfConversion.cpp b/llvm/lib/CodeGen/EarlyIfConversion.cpp
index 5f3e85077cb563..6824a4c73a14c1 100644
--- a/llvm/lib/CodeGen/EarlyIfConversion.cpp
+++ b/llvm/lib/CodeGen/EarlyIfConversion.cpp
@@ -180,6 +180,13 @@ class SSAIfConv {
/// speculatively execute it.
bool canConvertIf(MachineBasicBlock *MBB, bool Predicate = false);
+ /// isProfitableToConvertIf - Apply the target heuristic to decide if the
+ /// transformation is profitable. Branch probabilities are considered
+ /// currently.
+ bool isProfitableToConvertIf(const MachineBranchProbabilityInfo *MBPI,
+ TargetSchedModel SchedModel,
+ bool Predicate = false);
+
/// convertIf - If-convert the last block passed to canConvertIf(), assuming
/// it is possible. Add any erased blocks to RemovedBlocks.
void convertIf(SmallVectorImpl<MachineBasicBlock *> &RemovedBlocks,
@@ -561,6 +568,46 @@ bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB, bool Predicate) {
return true;
}
+/// Apply the target heuristic to decide if the transformation is profitable.
+bool SSAIfConv::isProfitableToConvertIf(
+ const MachineBranchProbabilityInfo *MBPI, TargetSchedModel SchedModel,
+ bool Predicate) {
+ auto TrueProbability = MBPI->getEdgeProbability(Head, TBB);
+ if (isTriangle()) {
+ MachineBasicBlock &IfBlock = (TBB == Tail) ? *FBB : *TBB;
+
+ unsigned ExtraPredCost = 0;
+ unsigned Cycles = 0;
+ for (MachineInstr &I : IfBlock) {
+ unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
+ if (NumCycles > 1)
+ Cycles += NumCycles - 1;
+ ExtraPredCost += TII->getPredicationCost(I);
+ }
+
+ return TII->isProfitableToIfCvt(IfBlock, Cycles, ExtraPredCost,
+ TrueProbability);
+ }
+ unsigned TExtra = 0;
+ unsigned FExtra = 0;
+ unsigned TCycle = 0;
+ unsigned FCycle = 0;
+ for (MachineInstr &I : *TBB) {
+ unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
+ if (NumCycles > 1)
+ TCycle += NumCycles - 1;
+ TExtra += TII->getPredicationCost(I);
+ }
+ for (MachineInstr &I : *FBB) {
+ unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
+ if (NumCycles > 1)
+ FCycle += NumCycles - 1;
+ FExtra += TII->getPredicationCost(I);
+ }
+ return TII->isProfitableToIfCvt(*TBB, TCycle, TExtra, *FBB, FCycle, FExtra,
+ TrueProbability);
+}
+
/// \return true iff the two registers are known to have the same value.
static bool hasSameValue(const MachineRegisterInfo &MRI,
const TargetInstrInfo *TII, Register TReg,
@@ -1155,43 +1202,8 @@ void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
MachineFunctionPass::getAnalysisUsage(AU);
}
-/// Apply the target heuristic to decide if the transformation is profitable.
bool EarlyIfPredicator::shouldConvertIf() {
- auto TrueProbability = MBPI->getEdgeProbability(IfConv.Head, IfConv.TBB);
- if (IfConv.isTriangle()) {
- MachineBasicBlock &IfBlock =
- (IfConv.TBB == IfConv.Tail) ? *IfConv.FBB : *IfConv.TBB;
-
- unsigned ExtraPredCost = 0;
- unsigned Cycles = 0;
- for (MachineInstr &I : IfBlock) {
- unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
- if (NumCycles > 1)
- Cycles += NumCycles - 1;
- ExtraPredCost += TII->getPredicationCost(I);
- }
-
- return TII->isProfitableToIfCvt(IfBlock, Cycles, ExtraPredCost,
- TrueProbability);
- }
- unsigned TExtra = 0;
- unsigned FExtra = 0;
- unsigned TCycle = 0;
- unsigned FCycle = 0;
- for (MachineInstr &I : *IfConv.TBB) {
- unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
- if (NumCycles > 1)
- TCycle += NumCycles - 1;
- TExtra += TII->getPredicationCost(I);
- }
- for (MachineInstr &I : *IfConv.FBB) {
- unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
- if (NumCycles > 1)
- FCycle += NumCycles - 1;
- FExtra += TII->getPredicationCost(I);
- }
- return TII->isProfitableToIfCvt(*IfConv.TBB, TCycle, TExtra, *IfConv.FBB,
- FCycle, FExtra, TrueProbability);
+ return IfConv.isProfitableToConvertIf(MBPI, SchedModel, /*Predicate=*/true);
}
/// Attempt repeated if-conversion on MBB, return true if successful.
More information about the llvm-commits
mailing list