[llvm] [InstCombine] Avoid propagating invalid metadata in FoldOpIntoSelect (PR #199155)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 01:19:48 PDT 2026
================
@@ -1831,7 +1831,24 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
NewTV = foldOperationIntoSelectOperand(Op, SI, TV, *this);
if (!NewFV)
NewFV = foldOperationIntoSelectOperand(Op, SI, FV, *this);
- return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
+
+ SelectInst *NewSel = SelectInst::Create(SI->getCondition(), NewTV, NewFV);
+
+ // Preserve metadata that remains valid for the transformed select.
+ if (MDNode *Prof = SI->getMetadata(LLVMContext::MD_prof))
+ NewSel->setMetadata(LLVMContext::MD_prof, Prof);
+ if (MDNode *Unpred = SI->getMetadata(LLVMContext::MD_unpredictable))
+ NewSel->setMetadata(LLVMContext::MD_unpredictable, Unpred);
+
+ // Preserve !fpmath only for FP-typed selects.
+ if (isa<FPMathOperator>(NewSel))
+ if (MDNode *FPMath = SI->getMetadata(LLVMContext::MD_fpmath))
+ NewSel->setMetadata(LLVMContext::MD_fpmath, FPMath);
----------------
nikic wrote:
I don't think `!fpmath` on a select really makes sense, because they are precise bitwise copies, so talking about acceptable error for them is meaningless. Probably not worth trying to forbid it, but we don't need to go out of the way to copy it.
https://github.com/llvm/llvm-project/pull/199155
More information about the llvm-commits
mailing list