diff options
author | Tianqing Wang <tianqing.wang@intel.com> | 2024-07-23 07:47:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-23 07:47:21 +0800 |
commit | 3d494bfc7ff73cb0a8dbe16ac41db6f47910eef1 (patch) | |
tree | 964a922cb273e0a08abd28b7ba73cd03cd9f0185 /llvm/lib/Transforms/Utils/SimplifyCFG.cpp | |
parent | b6dbda67d8f687350de66e68a7fd61433fac7107 (diff) | |
download | llvm-3d494bfc7ff73cb0a8dbe16ac41db6f47910eef1.zip llvm-3d494bfc7ff73cb0a8dbe16ac41db6f47910eef1.tar.gz llvm-3d494bfc7ff73cb0a8dbe16ac41db6f47910eef1.tar.bz2 |
[SimplifyCFG] Increase budget for FoldTwoEntryPHINode() if the branch is unpredictable. (#98495)
The `!unpredictable` metadata has been present for a long time, but
it's usage in optimizations is still limited. This patch teaches
`FoldTwoEntryPHINode()` to be more aggressive with an unpredictable
branch to reduce mispredictions.
A TTI interface `getBranchMispredictPenalty()` is added to distinguish
between different hardwares to ensure we don't go too far for simpler
cores. For simplicity, only a naive x86 implementation is included for
the time being.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 8f717cb..f23e288 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3476,7 +3476,8 @@ static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI, /// Given a BB that starts with the specified two-entry PHI node, /// see if we can eliminate it. static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, - DomTreeUpdater *DTU, const DataLayout &DL) { + DomTreeUpdater *DTU, const DataLayout &DL, + bool SpeculateUnpredictables) { // Ok, this is a two entry PHI node. Check to see if this is a simple "if // statement", which has a very simple dominance structure. Basically, we // are trying to find the condition that is being branched on, which @@ -3508,7 +3509,8 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, // jump to one specific 'then' block (if we have two of them). // It isn't beneficial to speculatively execute the code // from the block that we know is predictably not entered. - if (!DomBI->getMetadata(LLVMContext::MD_unpredictable)) { + bool IsUnpredictable = DomBI->getMetadata(LLVMContext::MD_unpredictable); + if (!IsUnpredictable) { uint64_t TWeight, FWeight; if (extractBranchWeights(*DomBI, TWeight, FWeight) && (TWeight + FWeight) != 0) { @@ -3551,6 +3553,8 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, InstructionCost Cost = 0; InstructionCost Budget = TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; + if (SpeculateUnpredictables && IsUnpredictable) + Budget += TTI.getBranchMispredictPenalty(); bool Changed = false; for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) { @@ -3620,8 +3624,9 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, [](BasicBlock *IfBlock) { return IfBlock->hasAddressTaken(); })) return Changed; - LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond - << " T: " << IfTrue->getName() + LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond; + if (IsUnpredictable) dbgs() << " (unpredictable)"; + dbgs() << " T: " << IfTrue->getName() << " F: " << IfFalse->getName() << "\n"); // If we can still promote the PHI nodes after this gauntlet of tests, @@ -7814,7 +7819,8 @@ bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) { // eliminate it, do so now. if (auto *PN = dyn_cast<PHINode>(BB->begin())) if (PN->getNumIncomingValues() == 2) - if (FoldTwoEntryPHINode(PN, TTI, DTU, DL)) + if (FoldTwoEntryPHINode(PN, TTI, DTU, DL, + Options.SpeculateUnpredictables)) return true; } |