[llvm] [profcheck] Fix profile metadata in IntegerDivision/ExpandIRinsts (PR #173114)
Jin Huang via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 22 11:34:33 PST 2025
================
@@ -235,11 +244,53 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
Value *SR = Builder.CreateSub(Tmp0, Tmp1);
Value *Ret0_4 = Builder.CreateICmpUGT(SR, MSB);
+
+ // Add 'unlikely' branch weights to the select instruction ('Ret0') generated
+ // by 'CreateLogicalOr'. We assume the 'true' path, where either the divisor
+ // or dividend is zero ('Ret0_3'), is less likely than the 'false' path,
+ // which corresponds to the |divisor| > |dividend| ('Ret0_4').
Value *Ret0 = Builder.CreateLogicalOr(Ret0_3, Ret0_4);
+ if (!ProfcheckDisableMetadataFixes) {
+ if (Instruction *Ret0SelectI = dyn_cast<Instruction>(Ret0)) {
+ Ret0SelectI->setMetadata(
+ LLVMContext::MD_prof,
+ MDBuilder(Ret0SelectI->getContext()).createUnlikelyBranchWeights());
+ }
+ }
Value *RetDividend = Builder.CreateICmpEQ(SR, MSB);
- Value *RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
- Value *EarlyRet = Builder.CreateLogicalOr(Ret0, RetDividend);
- Builder.CreateCondBr(EarlyRet, End, BB1);
+
+ // This select instruction (RetVal) immediately follows the Ret0 check. Since
+ // Ret0_4 determines if the divisor is greater then the dividend so the
+ // unknown (50/50) branch weights are fairly accurate for RetVal.
----------------
jinhuang1102 wrote:
> both the dividend and the divisor are being randomly sampled from the same distribution...
Yes, this was my assumptoion.
> I would expect the common case to be that the dividend is greater than the divisor.
I agree. That makes sense.
https://github.com/llvm/llvm-project/pull/173114
More information about the llvm-commits
mailing list