[llvm] [InstCombine] Preserve profile data with select instructions and binary operators (PR #158375)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 12 15:42:47 PDT 2025
================
@@ -1381,24 +1384,28 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
// We need an 'add' and exactly 1 arm of the select to have been simplified.
if (Opcode != Instruction::Add || (!True && !False) || (True && False))
return nullptr;
-
- Value *N;
+ Value *N, *SI = nullptr;
if (True && match(FVal, m_Neg(m_Value(N)))) {
Value *Sub = Builder.CreateSub(Z, N);
- return Builder.CreateSelect(Cond, True, Sub, I.getName());
+ SI = Builder.CreateSelect(Cond, True, Sub, I.getName());
}
if (False && match(TVal, m_Neg(m_Value(N)))) {
Value *Sub = Builder.CreateSub(Z, N);
- return Builder.CreateSelect(Cond, Sub, False, I.getName());
+ SI = Builder.CreateSelect(Cond, Sub, False, I.getName());
}
- return nullptr;
+ if (!ProfcheckDisableMetadataFixes && SI)
+ cast<SelectInst>(SI)->setMetadata(LLVMContext::MD_prof, ProfileData);
+ return SI;
};
if (LHSIsSelect && RHSIsSelect && A == D) {
// (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F)
Cond = A;
True = simplifyBinOp(Opcode, B, E, FMF, Q);
False = simplifyBinOp(Opcode, C, F, FMF, Q);
+ // Profile weights for both LHS and RHS should be the same because they have
----------------
mtrofin wrote:
there are 2 cases where the pointers won't be the same:
1) the branch weights are e.g. 2, 4 and 1, 2. So same probability, different expression
2) the metadata was added as unique, and then you'd have 2 different objects, content-wise identical
IDK, may be safer to LLVM_DEBUG. The assert may get someone on a wild goose chase in the cases above. Or add a comment above it that we don't expect those cases to happen, but if they do, safe to replace the assert w a LLVM_DEBUG or something of the sort.
https://github.com/llvm/llvm-project/pull/158375
More information about the llvm-commits
mailing list