[llvm] [InstCombine][profcheck] Propogate profile metadata when transforming br (X && !Y) to br (!X || Y) (PR #175939)

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 26 14:52:25 PST 2026


https://github.com/snehasish updated https://github.com/llvm/llvm-project/pull/175939

>From 5d724b0b7f4549265a5aa17acb1be97ce38012db Mon Sep 17 00:00:00 2001
From: Snehasish Kumar <snehasishk at google.com>
Date: Wed, 14 Jan 2026 12:12:06 +0000
Subject: [PATCH] [InstCombine][profcheck] Propogate profile metadata when
 transforming br (X && !Y) to br (!X || Y)

Updated visitBranchInst to propagate and swap !prof metadata when transforming br (X && !Y) to br (!X || Y).
---
 .../InstCombine/InstructionCombining.cpp      | 15 ++++++++
 llvm/test/Transforms/InstCombine/branch.ll    | 34 ++++++++++++-------
 llvm/utils/profcheck-xfail.txt                |  1 -
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 229f0c01a0e42..d0f3450f9878f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -4278,6 +4278,21 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
             m_OneUse(m_LogicalAnd(m_Value(X), m_OneUse(m_Not(m_Value(Y))))))) {
     Value *NotX = Builder.CreateNot(X, "not." + X->getName());
     Value *Or = Builder.CreateLogicalOr(NotX, Y);
+
+    // Set weights for the new OR select instruction too.
+    if (!ProfcheckDisableMetadataFixes) {
+      if (auto *OrInst = dyn_cast<Instruction>(Or)) {
+        if (auto *CondInst = dyn_cast<Instruction>(Cond)) {
+          SmallVector<uint32_t> Weights;
+          if (extractBranchWeights(*CondInst, Weights)) {
+            assert(Weights.size() == 2 &&
+                   "Unexpected number of branch weights!");
+            std::swap(Weights[0], Weights[1]);
+            setBranchWeights(*OrInst, Weights, /*IsExpected=*/false);
+          }
+        }
+      }
+    }
     BI.swapSuccessors();
     if (BPI)
       BPI->swapSuccEdgesProbabilities(BI.getParent());
diff --git a/llvm/test/Transforms/InstCombine/branch.ll b/llvm/test/Transforms/InstCombine/branch.ll
index 1d5ff72eef9ce..09bba16e9e6c7 100644
--- a/llvm/test/Transforms/InstCombine/branch.ll
+++ b/llvm/test/Transforms/InstCombine/branch.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 smart
 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
 
 declare void @use(i1)
@@ -121,8 +121,8 @@ define i32 @logical_and_not(i1 %x, i1 %y) {
 ; CHECK-LABEL: @logical_and_not(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
-; CHECK-NEXT:    [[TMP0:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]]
-; CHECK-NEXT:    br i1 [[TMP0]], label [[F:%.*]], label [[T:%.*]]
+; CHECK-NEXT:    [[TMP0:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]], !prof [[PROF0:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP0]], label [[F:%.*]], label [[T:%.*]], !prof [[PROF0]]
 ; CHECK:       t:
 ; CHECK-NEXT:    ret i32 42
 ; CHECK:       f:
@@ -130,8 +130,8 @@ define i32 @logical_and_not(i1 %x, i1 %y) {
 ;
 entry:
   %noty = xor i1 %y, true
-  %and = select i1 %x, i1 %noty, i1 false
-  br i1 %and, label %t, label %f
+  %and = select i1 %x, i1 %noty, i1 false, !prof !0
+  br i1 %and, label %t, label %f, !prof !0
 
 t:
   ret i32 42
@@ -140,22 +140,24 @@ f:
   ret i32 3
 }
 
+!0 = !{!"branch_weights", i32 10, i32 20}
+
 ; if (x && y || !x) ret 3; ret 42 --> if (!x || y) ret 3; ret 42
 
 define i32 @logical_and_or(i1 %x, i1 %y) {
 ; CHECK-LABEL: @logical_and_or(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
-; CHECK-NEXT:    [[AND:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]]
-; CHECK-NEXT:    br i1 [[AND]], label [[F:%.*]], label [[T:%.*]]
+; CHECK-NEXT:    [[AND:%.*]] = select i1 [[NOT_X]], i1 true, i1 [[Y:%.*]], !prof [[PROF0]]
+; CHECK-NEXT:    br i1 [[AND]], label [[F:%.*]], label [[T:%.*]], !prof [[PROF1:![0-9]+]]
 ; CHECK:       t:
 ; CHECK-NEXT:    ret i32 42
 ; CHECK:       f:
 ; CHECK-NEXT:    ret i32 3
 ;
 entry:
-  %and = select i1 %x, i1 %y, i1 true
-  br i1 %and, label %f, label %t
+  %and = select i1 %x, i1 %y, i1 true, !prof !0
+  br i1 %and, label %f, label %t, !prof !0
 
 t:
   ret i32 42
@@ -170,8 +172,8 @@ define i32 @logical_or_not(i1 %x, i1 %y) {
 ; CHECK-LABEL: @logical_or_not(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[NOTX:%.*]] = xor i1 [[X:%.*]], true
-; CHECK-NEXT:    [[AND:%.*]] = select i1 [[NOTX]], i1 true, i1 [[Y:%.*]]
-; CHECK-NEXT:    br i1 [[AND]], label [[F:%.*]], label [[T:%.*]]
+; CHECK-NEXT:    [[AND:%.*]] = select i1 [[NOTX]], i1 true, i1 [[Y:%.*]], !prof [[PROF1]]
+; CHECK-NEXT:    br i1 [[AND]], label [[F:%.*]], label [[T:%.*]], !prof [[PROF1]]
 ; CHECK:       t:
 ; CHECK-NEXT:    ret i32 42
 ; CHECK:       f:
@@ -179,8 +181,8 @@ define i32 @logical_or_not(i1 %x, i1 %y) {
 ;
 entry:
   %notx = xor i1 %x, true
-  %and = select i1 %notx, i1 true, i1 %y
-  br i1 %and, label %f, label %t
+  %and = select i1 %notx, i1 true, i1 %y, !prof !0
+  br i1 %and, label %f, label %t, !prof !0
 
 t:
   ret i32 42
@@ -338,3 +340,9 @@ if.else:
   %zext = zext i1 %cmp to i32
   ret i32 %zext
 }
+
+
+;.
+; CHECK: [[PROF0]] = !{!"branch_weights", i32 20, i32 10}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 10, i32 20}
+;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 8d381bbc3c152..9acd9452d506f 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -232,7 +232,6 @@ Transforms/InstCombine/binop-select-cast-of-select-cond.ll
 Transforms/InstCombine/binop-select.ll
 Transforms/InstCombine/bit-checks.ll
 Transforms/InstCombine/bitreverse.ll
-Transforms/InstCombine/branch.ll
 Transforms/InstCombine/builtin-dynamic-object-size.ll
 Transforms/InstCombine/canonicalize-clamp-like-pattern-between-negative-and-positive-thresholds.ll
 Transforms/InstCombine/canonicalize-clamp-like-pattern-between-zero-and-positive-threshold.ll



More information about the llvm-commits mailing list