[llvm] [VPlan] Use BlockFrequencyInfo in getPredBlockCostDivisor (PR #158690)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 25 02:31:19 PST 2025


================
@@ -2886,6 +2884,23 @@ bool LoopVectorizationCostModel::isPredicatedInst(Instruction *I) const {
   }
 }
 
+unsigned LoopVectorizationCostModel::getPredBlockCostDivisor(
+    TargetTransformInfo::TargetCostKind CostKind, const BasicBlock *BB) const {
+  // If the block wasn't originally predicated then return early to avoid
+  // computing BlockFrequencyInfo unnecessarily.
+  if (!Legal->blockNeedsPredication(BB))
+    return 1;
+  if (CostKind == TTI::TCK_CodeSize)
+    return 1;
+
+  BlockFrequencyInfo *BFI = GetBFI();
+  uint64_t HeaderFreq = BFI->getBlockFreq(TheLoop->getHeader()).getFrequency();
+  uint64_t BBFreq = BFI->getBlockFreq(BB).getFrequency();
+  assert(HeaderFreq >= BBFreq &&
+         "Header has smaller block freq than dominated BB?");
+  return HeaderFreq / BBFreq;
----------------
david-arm wrote:

If I understand correctly the header frequency should really be the sum of all block frequencies in the loop? That's because it dominates all the other blocks, so in order to get to any block in the loop it has to go through the header. If so, then this calculation looks right to me.

https://github.com/llvm/llvm-project/pull/158690


More information about the llvm-commits mailing list