[llvm] [profcheck][CVP] Adding 'unknown' branch weights for `urem` expansion in CVP (PR #180637)
Jin Huang via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 13 15:58:53 PST 2026
https://github.com/jinhuang1102 updated https://github.com/llvm/llvm-project/pull/180637
>From cdad5b71d556a89355976058b2e4c48365d45df2 Mon Sep 17 00:00:00 2001
From: Jin Huang <jingold at google.com>
Date: Mon, 9 Feb 2026 23:02:10 +0000
Subject: [PATCH] [profcheck] Adding 'unkonwn' branch weights for expanding
urem in CVP
---
.../Scalar/CorrelatedValuePropagation.cpp | 9 +++-
.../CorrelatedValuePropagation/cond-at-use.ll | 15 ------
.../urem-expansion.ll | 53 +++++--------------
.../CorrelatedValuePropagation/urem.ll | 14 +++--
llvm/utils/profcheck-xfail.txt | 3 --
5 files changed, 32 insertions(+), 62 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 4627f537dc16b..09e52462d182d 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -32,9 +32,11 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
@@ -83,7 +85,7 @@ STATISTIC(NumShlNUW, "Number of no-unsigned-wrap deductions for shl");
STATISTIC(NumAbs, "Number of llvm.abs intrinsics removed");
STATISTIC(NumOverflows, "Number of overflow checks removed");
STATISTIC(NumSaturating,
- "Number of saturating arithmetics converted to normal arithmetics");
+ "Number of saturating arithmetics converted to normal arithmetics");
STATISTIC(NumNonNull, "Number of function pointer arguments marked non-null");
STATISTIC(NumCmpIntr, "Number of llvm.[us]cmp intrinsics removed");
STATISTIC(NumMinMax, "Number of llvm.[us]{min,max} intrinsics removed");
@@ -878,6 +880,11 @@ static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
auto *Cmp = B.CreateICmp(ICmpInst::ICMP_ULT, FrozenX, FrozenY,
Instr->getName() + ".cmp");
ExpandedOp = B.CreateSelect(Cmp, FrozenX, AdjX);
+ // Test Unlikely Branch Weights.
+ applyProfMetadataIfEnabled(ExpandedOp, [&](Instruction *Inst) {
+ setExplicitlyUnknownBranchWeightsIfProfiled(*Inst, DEBUG_TYPE,
+ Instr->getFunction());
+ });
} else {
auto *Cmp =
B.CreateICmp(ICmpInst::ICMP_UGE, X, Y, Instr->getName() + ".cmp");
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll b/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
index fa9323df4dff7..04a8338d39949 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/cond-at-use.ll
@@ -344,21 +344,6 @@ define i16 @urem_elide(i16 noundef %x) {
ret i16 %sel
}
-define i16 @urem_expand(i16 noundef %x) {
-; CHECK-LABEL: @urem_expand(
-; CHECK-NEXT: [[UREM_UREM:%.*]] = sub nuw i16 [[X:%.*]], 42
-; CHECK-NEXT: [[UREM_CMP:%.*]] = icmp ult i16 [[X]], 42
-; CHECK-NEXT: [[UREM:%.*]] = select i1 [[UREM_CMP]], i16 [[X]], i16 [[UREM_UREM]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i16 [[X]], 84
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i16 [[UREM]], i16 24
-; CHECK-NEXT: ret i16 [[SEL]]
-;
- %urem = urem i16 %x, 42
- %cmp = icmp ult i16 %x, 84
- %sel = select i1 %cmp, i16 %urem, i16 24
- ret i16 %sel
-}
-
define i16 @urem_narrow(i16 noundef %x) {
; CHECK-LABEL: @urem_narrow(
; CHECK-NEXT: [[UREM_LHS_TRUNC:%.*]] = trunc i16 [[X:%.*]] to i8
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll b/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
index c6e4265f855e0..12588464edc7d 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/urem-expansion.ll
@@ -1,21 +1,10 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
declare void @llvm.assume(i1)
; Divisor is constant. X's range is known
-define i8 @constant.divisor.v3(i8 %x) {
-; CHECK-LABEL: @constant.divisor.v3(
-; CHECK-NEXT: [[CMP_X_UPPER:%.*]] = icmp ult i8 [[X:%.*]], 3
-; CHECK-NEXT: call void @llvm.assume(i1 [[CMP_X_UPPER]])
-; CHECK-NEXT: ret i8 [[X]]
-;
- %cmp.x.upper = icmp ult i8 %x, 3
- call void @llvm.assume(i1 %cmp.x.upper)
- %rem = urem i8 %x, 3
- ret i8 %rem
-}
define i8 @constant.divisor.v4(i8 %x) {
; CHECK-LABEL: @constant.divisor.v4(
; CHECK-NEXT: [[CMP_X_UPPER:%.*]] = icmp ult i8 [[X:%.*]], 4
@@ -31,19 +20,7 @@ define i8 @constant.divisor.v4(i8 %x) {
%rem = urem i8 %x, 3
ret i8 %rem
}
-define i8 @constant.divisor.x.range.v4(ptr %x.ptr) {
-; CHECK-LABEL: @constant.divisor.x.range.v4(
-; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG0:![0-9]+]]
-; CHECK-NEXT: [[X_FROZEN:%.*]] = freeze i8 [[X]]
-; CHECK-NEXT: [[REM_UREM:%.*]] = sub nuw i8 [[X_FROZEN]], 3
-; CHECK-NEXT: [[REM_CMP:%.*]] = icmp ult i8 [[X_FROZEN]], 3
-; CHECK-NEXT: [[REM:%.*]] = select i1 [[REM_CMP]], i8 [[X_FROZEN]], i8 [[REM_UREM]]
-; CHECK-NEXT: ret i8 [[REM]]
-;
- %x = load i8, ptr %x.ptr, !range !{ i8 0, i8 4 }
- %rem = urem i8 %x, 3
- ret i8 %rem
-}
+
define i8 @constant.divisor.x.mask.v4(i8 %x) {
; CHECK-LABEL: @constant.divisor.x.mask.v4(
; CHECK-NEXT: [[X_MASKED:%.*]] = and i8 [[X:%.*]], 3
@@ -215,7 +192,7 @@ define i8 @variable.v4(i8 %x, i8 %y) {
}
define i8 @variable.v4.range(ptr %x.ptr, ptr %y.ptr) {
; CHECK-LABEL: @variable.v4.range(
-; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG0]]
+; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG0:![0-9]+]]
; CHECK-NEXT: [[Y:%.*]] = load i8, ptr [[Y_PTR:%.*]], align 1, !range [[RNG1:![0-9]+]]
; CHECK-NEXT: [[X_FROZEN:%.*]] = freeze i8 [[X]]
; CHECK-NEXT: [[Y_FROZEN:%.*]] = freeze i8 [[Y]]
@@ -351,19 +328,6 @@ define i8 @large.divisor.v1(i8 %x) {
%rem = urem i8 %x, 127
ret i8 %rem
}
-define i8 @large.divisor.v1.range(ptr %x.ptr) {
-; CHECK-LABEL: @large.divisor.v1.range(
-; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG2:![0-9]+]]
-; CHECK-NEXT: [[X_FROZEN:%.*]] = freeze i8 [[X]]
-; CHECK-NEXT: [[REM_UREM:%.*]] = sub nuw i8 [[X_FROZEN]], 127
-; CHECK-NEXT: [[REM_CMP:%.*]] = icmp ult i8 [[X_FROZEN]], 127
-; CHECK-NEXT: [[REM:%.*]] = select i1 [[REM_CMP]], i8 [[X_FROZEN]], i8 [[REM_UREM]]
-; CHECK-NEXT: ret i8 [[REM]]
-;
- %x = load i8, ptr %x.ptr, !range !{ i8 0, i8 128 }
- %rem = urem i8 %x, 127
- ret i8 %rem
-}
define i8 @large.divisor.v2.unbound.x(i8 %x) {
; CHECK-LABEL: @large.divisor.v2.unbound.x(
; CHECK-NEXT: [[REM:%.*]] = urem i8 [[X:%.*]], 127
@@ -401,7 +365,7 @@ define i8 @large.divisor.with.overflow.v1(i8 %x) {
}
define i8 @large.divisor.with.overflow.v1.range(ptr %x.ptr) {
; CHECK-LABEL: @large.divisor.with.overflow.v1.range(
-; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG3:![0-9]+]]
+; CHECK-NEXT: [[X:%.*]] = load i8, ptr [[X_PTR:%.*]], align 1, !range [[RNG2:![0-9]+]]
; CHECK-NEXT: [[X_FROZEN:%.*]] = freeze i8 [[X]]
; CHECK-NEXT: [[REM_UREM:%.*]] = sub nuw i8 [[X_FROZEN]], -128
; CHECK-NEXT: [[REM_CMP:%.*]] = icmp ult i8 [[X_FROZEN]], -128
@@ -456,3 +420,12 @@ define i8 @known_uge(i8 noundef %x) {
%rem = urem i8 %x, 3
ret i8 %rem
}
+
+!0 = !{!"function_entry_count", i64 1000}
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
+;.
+; CHECK: [[RNG0]] = !{i8 0, i8 4}
+; CHECK: [[RNG1]] = !{i8 3, i8 5}
+; CHECK: [[RNG2]] = !{i8 0, i8 -127}
+;.
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll b/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
index e69deaa73d73b..0073a714a199a 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/urem.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
define void @test_nop(i32 %n) {
@@ -114,13 +114,13 @@ exit:
ret void
}
-define void @test5(i32 %n) {
+define void @test5(i32 %n) !prof !0 {
; CHECK-LABEL: @test5(
; CHECK-NEXT: [[TRUNC:%.*]] = and i32 [[N:%.*]], 63
; CHECK-NEXT: [[TRUNC_FROZEN:%.*]] = freeze i32 [[TRUNC]]
; CHECK-NEXT: [[DIV_UREM:%.*]] = sub nuw i32 [[TRUNC_FROZEN]], 42
; CHECK-NEXT: [[DIV_CMP:%.*]] = icmp ult i32 [[TRUNC_FROZEN]], 42
-; CHECK-NEXT: [[DIV:%.*]] = select i1 [[DIV_CMP]], i32 [[TRUNC_FROZEN]], i32 [[DIV_UREM]]
+; CHECK-NEXT: [[DIV:%.*]] = select i1 [[DIV_CMP]], i32 [[TRUNC_FROZEN]], i32 [[DIV_UREM]], !prof [[PROF1:![0-9]+]]
; CHECK-NEXT: ret void
;
%trunc = and i32 %n, 63
@@ -472,3 +472,11 @@ define i1 @urem_i1() {
}
declare void @use(i1)
+
+!0 = !{!"function_entry_count", i64 1000}
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
+;.
+; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF1]] = !{!"unknown", !"correlated-value-propagation"}
+;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 454f3d8f8fe00..59e9abdf1415c 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -60,9 +60,6 @@ Transforms/Coroutines/coro-split-tbaa-md-neg.ll
Transforms/Coroutines/coro-split-tbaa-md.ll
Transforms/Coroutines/coro-split-noinline.ll
Transforms/Coroutines/no-suspend.ll
-Transforms/CorrelatedValuePropagation/cond-at-use.ll
-Transforms/CorrelatedValuePropagation/urem-expansion.ll
-Transforms/CorrelatedValuePropagation/urem.ll
Transforms/CrossDSOCFI/basic.ll
Transforms/CrossDSOCFI/cfi_functions.ll
Transforms/CrossDSOCFI/thumb.ll
More information about the llvm-commits
mailing list