[llvm] [InstCombine] Fix profile metadata when folding implied conditionals (PR #170756)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 4 13:59:08 PST 2025
https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/170756
\#163412 touched this last and directly propagated the profile information. This was not correct for the motivating example:
%a = icmp eq i32 %z, 0
%b = icmp eq i32 %z, 1
%v2 = select i1 %b, i1 true, i1 %pred, !prof !18
%v3 = and i1 %a, %v2
to
%a = icmp eq i32 %z, 0
%v3 = select i1 %a, i1 %pred, i1 false
z == 1 does not imply that z == 0 for i8. In general for the and case, we need a => b, which means that b must be equivalent or more restrictive than a, which means we cannot propagate profile information without additional information on the value distribution of z. For the or case we need !a => b. We again cannot derive profile information for a/!a without additional value distribution information.
>From 7eb4e871d3e40171f9bb7c74f00463c5f61bd1fa Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Thu, 4 Dec 2025 18:57:22 +0000
Subject: [PATCH] [InstCombine] Fix profile metadata when folding implied
conditionals
\#163412 touched this last and directly propagated the profile
information. This was not correct for the motivating example:
%a = icmp eq i32 %z, 0
%b = icmp eq i32 %z, 1
%v2 = select i1 %b, i1 true, i1 %pred, !prof !18
%v3 = and i1 %a, %v2
to
%a = icmp eq i32 %z, 0
%v3 = select i1 %a, i1 %pred, i1 false
z == 1 does not imply that z == 0 for i8. In general for the and case,
we need a => b, which means that b must be equivalent or more restrictive
than a, which means we cannot propagate profile information without
additional information on the value distribution of z. For the or case
we need !a => b. We again cannot derive profile information for a/!a
without additional value distribution information.
---
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 10 +++-------
.../InstCombine/select-safe-impliedcond-transforms.ll | 2 +-
2 files changed, 4 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index c9f51e4b294b1..c00551bc1f939 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3067,14 +3067,10 @@ Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
"Op must be either i1 or vector of i1.");
if (SI.getCondition()->getType() != Op->getType())
return nullptr;
- if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL)) {
- Instruction *MDFrom = nullptr;
- if (!ProfcheckDisableMetadataFixes)
- MDFrom = &SI;
- return SelectInst::Create(
+ if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL))
+ return createSelectInstWithUnknownProfile(
Op, IsAnd ? V : ConstantInt::getTrue(Op->getType()),
- IsAnd ? ConstantInt::getFalse(Op->getType()) : V, "", nullptr, MDFrom);
- }
+ IsAnd ? ConstantInt::getFalse(Op->getType()) : V);
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/select-safe-impliedcond-transforms.ll b/llvm/test/Transforms/InstCombine/select-safe-impliedcond-transforms.ll
index bc988a9bbab04..d783cbf25de28 100644
--- a/llvm/test/Transforms/InstCombine/select-safe-impliedcond-transforms.ll
+++ b/llvm/test/Transforms/InstCombine/select-safe-impliedcond-transforms.ll
@@ -263,5 +263,5 @@ define i1 @neg_icmp_eq_implies_trunc(i8 %x, i1 %c) {
!1 = !{!"branch_weights", i32 2, i32 3}
;.
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
-; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
+; CHECK: [[PROF1]] = !{!"unknown", !"instcombine"}
;.
More information about the llvm-commits
mailing list