[llvm] [profcheck] Fix profile metadata in IntegerDivision/ExpandIRinsts (PR #173114)

Jin Huang via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 5 10:49:32 PST 2026


================
@@ -235,11 +245,50 @@ 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. We mark the case where either the divisor
+  // or the dividend is equal to zero as unlikely.
   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);
+
+  // Add 'unlikely' branch weights. We mark the case where the divisor is
----------------
jinhuang1102 wrote:

Sure. Deleted this comment because code itself already presents the logic.

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


More information about the llvm-commits mailing list