[llvm] [profcheck] Add heuristical profile metadata for lowering table-based cttz. (PR #161898)

Jin Huang via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 01:35:33 PDT 2025


================
@@ -599,6 +607,19 @@ static bool tryToRecognizeTableBasedCttz(Instruction &I, const DataLayout &DL) {
     auto Cmp = B.CreateICmpEQ(X1, ConstantInt::get(XType, 0));
     auto Select = B.CreateSelect(Cmp, B.CreateZExt(ZeroTableElem, XType), Cttz);
 
+    // Attach heuristic branch weigths to the newly 'select' instruction that
+    // handles the cttz(0) edge case The assumpltion is tht the input to a cttz
+    // operation is rarely 0, so we add a strong 100-to-1 bias weights to the
+    // 'false' path.
----------------
jinhuang1102 wrote:

> FWIW, we also have createUnlikelyBranchWeights() as the standard way to indicate an unlikely branch, though the values it uses are a lot more aggressive than 1:100.

Yes, [createUnlikelyBranchWeights()](https://github.com/llvm/llvm-project/blob/772071bb75772ddff43c9525f2b66077fd873c04/llvm/lib/IR/MDBuilder.cpp#L48) provides a more aggressive bias weights (1 vs. 1,048,575[1]).

[1] `(1U << 20) - 1`: shift 1 left 10 bit, then - 1

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


More information about the llvm-commits mailing list