[llvm] [InstCombine][profcheck] Fix missing branch data in InstCombineCompares.cpp (PR #178070)

Alan Zhao via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 26 14:50:20 PST 2026


https://github.com/alanzhao1 created https://github.com/llvm/llvm-project/pull/178070

These are trivial fixes where we create a new select instruction with the same conditional as an existing select.

Tracking issue: #147390

>From 54496aacd86bcbdf86990a998b3be9e1be12d378 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Mon, 26 Jan 2026 14:32:58 -0800
Subject: [PATCH] [InstCombine][profcheck] Fix missing branch data in
 InstCombineCompares.cpp

These are trivial fixes where we create a new select instruction with
the same conditional as an existing select.

Tracking issue: #147390
---
 .../InstCombine/InstCombineCompares.cpp       | 22 +++++++----
 .../Transforms/InstCombine/icmp-select.ll     | 37 +++++++++++++------
 llvm/utils/profcheck-xfail.txt                |  8 ----
 3 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0ca4e4495e6e4..a07af1aae94bb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -41,6 +41,10 @@ using namespace PatternMatch;
 // How many times is a select replaced by one of its operands?
 STATISTIC(NumSel, "Number of select opts");
 
+namespace llvm {
+extern cl::opt<bool> ProfcheckDisableMetadataFixes;
+}
+
 /// Compute Result = In1+In2, returning true if the result overflowed for this
 /// type.
 static bool addWithOverflow(APInt &Result, const APInt &In1, const APInt &In2,
@@ -4395,7 +4399,8 @@ Instruction *InstCombinerImpl::foldSelectICmp(CmpPredicate Pred, SelectInst *SI,
       Op1 = Builder.CreateICmp(Pred, SI->getOperand(1), RHS, I.getName());
     if (!Op2)
       Op2 = Builder.CreateICmp(Pred, SI->getOperand(2), RHS, I.getName());
-    return SelectInst::Create(SI->getOperand(0), Op1, Op2);
+    return SelectInst::Create(SI->getOperand(0), Op1, Op2, "", nullptr,
+                              ProfcheckDisableMetadataFixes ? nullptr : SI);
   }
 
   return nullptr;
@@ -5934,15 +5939,17 @@ enum class OffsetKind { Invalid, Value, Select };
 struct OffsetResult {
   OffsetKind Kind;
   Value *V0, *V1, *V2;
+  Instruction *MDFrom;
 
   static OffsetResult invalid() {
-    return {OffsetKind::Invalid, nullptr, nullptr, nullptr};
+    return {OffsetKind::Invalid, nullptr, nullptr, nullptr, nullptr};
   }
   static OffsetResult value(Value *V) {
-    return {OffsetKind::Value, V, nullptr, nullptr};
+    return {OffsetKind::Value, V, nullptr, nullptr, nullptr};
   }
-  static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV) {
-    return {OffsetKind::Select, Cond, TrueV, FalseV};
+  static OffsetResult select(Value *Cond, Value *TrueV, Value *FalseV,
+                             Instruction *MDFrom) {
+    return {OffsetKind::Select, Cond, TrueV, FalseV, MDFrom};
   }
   bool isValid() const { return Kind != OffsetKind::Invalid; }
   Value *materialize(InstCombiner::BuilderTy &Builder) const {
@@ -5952,7 +5959,8 @@ struct OffsetResult {
     case OffsetKind::Value:
       return V0;
     case OffsetKind::Select:
-      return Builder.CreateSelect(V0, V1, V2);
+      return Builder.CreateSelect(
+          V0, V1, V2, "", ProfcheckDisableMetadataFixes ? nullptr : MDFrom);
     }
     llvm_unreachable("Unknown OffsetKind enum");
   }
@@ -6018,7 +6026,7 @@ static Instruction *foldICmpEqualityWithOffset(ICmpInst &I,
       Value *FalseVal = ApplyOffsetImpl(Sel->getFalseValue(), BinOpc, RHS);
       if (!FalseVal)
         return OffsetResult::invalid();
-      return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal);
+      return OffsetResult::select(Sel->getCondition(), TrueVal, FalseVal, Sel);
     }
     if (Value *Simplified = ApplyOffsetImpl(V, BinOpc, RHS))
       return OffsetResult::value(Simplified);
diff --git a/llvm/test/Transforms/InstCombine/icmp-select.ll b/llvm/test/Transforms/InstCombine/icmp-select.ll
index c29527a3c3c5e..4b24f423841f0 100644
--- a/llvm/test/Transforms/InstCombine/icmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-select.ll
@@ -1,10 +1,13 @@
-; 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=instcombine < %s | FileCheck %s
 
 declare void @use(i8)
 declare void @use.i1(i1)
 declare i8 @llvm.umin.i8(i8, i8)
 
+;.
+; CHECK: @g = external global i8
+;.
 define i1 @icmp_select_const(i8 %x, i8 %y) {
 ; CHECK-LABEL: @icmp_select_const(
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
@@ -46,18 +49,18 @@ define i1 @icmp_select_var_commuted(i8 %x, i8 %y, i8 %_z) {
   ret i1 %cmp2
 }
 
-define i1 @icmp_select_var_select(i8 %x, i8 %y, i1 %c) {
+define i1 @icmp_select_var_select(i8 %x, i8 %y, i1 %c) !prof !0 {
 ; CHECK-LABEL: @icmp_select_var_select(
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[X:%.*]], 0
 ; CHECK-NEXT:    [[CMP212:%.*]] = icmp eq i8 [[X]], [[Y:%.*]]
 ; CHECK-NEXT:    [[NOT_C:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[CMP1]], i1 true, i1 [[NOT_C]]
-; CHECK-NEXT:    [[CMP2:%.*]] = select i1 [[TMP1]], i1 true, i1 [[CMP212]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[CMP1]], i1 true, i1 [[NOT_C]], !prof [[PROF1:![0-9]+]]
+; CHECK-NEXT:    [[CMP2:%.*]] = select i1 [[TMP1]], i1 true, i1 [[CMP212]], !prof [[PROF2:![0-9]+]]
 ; CHECK-NEXT:    ret i1 [[CMP2]]
 ;
-  %z = select i1 %c, i8 %x, i8 %y
+  %z = select i1 %c, i8 %x, i8 %y, !prof !1
   %cmp1 = icmp eq i8 %x, 0
-  %sel = select i1 %cmp1, i8 %z, i8 %y
+  %sel = select i1 %cmp1, i8 %z, i8 %y, !prof !2
   %cmp2 = icmp eq i8 %z, %sel
   ret i1 %cmp2
 }
@@ -600,23 +603,23 @@ define i1 @icmp_slt_select(i1 %cond, i32 %a, i32 %b) {
   ret i1 %res
 }
 
-define i1 @discr_eq(i8 %a, i8 %b) {
+define i1 @discr_eq(i8 %a, i8 %b) !prof !0 {
 ; CHECK-LABEL: @discr_eq(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt i8 [[A:%.*]], 1
 ; CHECK-NEXT:    [[CMP2:%.*]] = icmp ugt i8 [[B:%.*]], 1
-; CHECK-NEXT:    [[TMP0:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 3
-; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[CMP2]], i8 [[B]], i8 3
+; CHECK-NEXT:    [[TMP0:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 3, !prof [[PROF3:![0-9]+]]
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[CMP2]], i8 [[B]], i8 3, !prof [[PROF1]]
 ; CHECK-NEXT:    [[RES:%.*]] = icmp eq i8 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    ret i1 [[RES]]
 ;
 entry:
   %add1 = add i8 %a, -2
   %cmp1 = icmp ugt i8 %a, 1
-  %sel1 = select i1 %cmp1, i8 %add1, i8 1
+  %sel1 = select i1 %cmp1, i8 %add1, i8 1, !prof !1
   %add2 = add i8 %b, -2
   %cmp2 = icmp ugt i8 %b, 1
-  %sel2 = select i1 %cmp2, i8 %add2, i8 1
+  %sel2 = select i1 %cmp2, i8 %add2, i8 1, !prof !2
   %res = icmp eq i8 %sel1, %sel2
   ret i1 %res
 }
@@ -952,3 +955,15 @@ end:
   %cmp = icmp eq i32 %sel, 0
   ret i1 %cmp
 }
+
+!0 = !{!"function_entry_count", i64 1000}
+!1 = !{!"branch_weights", i32 2, i32 3}
+!2 = !{!"branch_weights", i32 5, i32 7}
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
+;.
+; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 5, i32 7}
+; CHECK: [[PROF2]] = !{!"unknown", !"instcombine"}
+; CHECK: [[PROF3]] = !{!"branch_weights", i32 2, i32 3}
+;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 8d381bbc3c152..5654de5e9872e 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -248,11 +248,7 @@ Transforms/InstCombine/ffs-i16.ll
 Transforms/InstCombine/fmul-bool.ll
 Transforms/InstCombine/fmul.ll
 Transforms/InstCombine/fneg.ll
-Transforms/InstCombine/icmp-and-lowbit-mask.ll
-Transforms/InstCombine/icmp.ll
 Transforms/InstCombine/icmp-mul-and.ll
-Transforms/InstCombine/icmp-select-implies-common-op.ll
-Transforms/InstCombine/icmp-select.ll
 Transforms/InstCombine/icmp-with-selects.ll
 Transforms/InstCombine/intrinsic-select.ll
 Transforms/InstCombine/ldexp-ext.ll
@@ -291,12 +287,8 @@ Transforms/InstCombine/pow-sqrt.ll
 Transforms/InstCombine/pull-conditional-binop-through-shift.ll
 Transforms/InstCombine/rem.ll
 Transforms/InstCombine/sdiv-canonicalize.ll
-Transforms/InstCombine/sdiv-guard.ll
 Transforms/InstCombine/select-and-or.ll
-Transforms/InstCombine/select-cmp-br.ll
-Transforms/InstCombine/select-cmp.ll
 Transforms/InstCombine/select-factorize.ll
-Transforms/InstCombine/select.ll
 Transforms/InstCombine/select-min-max.ll
 Transforms/InstCombine/select-of-symmetric-selects.ll
 Transforms/InstCombine/select-select.ll



More information about the llvm-commits mailing list