[llvm] [InstCombine] Copy metadata in select-to-masked.load fold (PR #211965)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 23:26:25 PDT 2026


================
@@ -5306,12 +5306,15 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
 
   Value *MaskedLoadPtr;
   if (match(TrueVal, m_OneUse(m_MaskedLoad(m_Value(MaskedLoadPtr),
-                                           m_Specific(CondVal), m_Value()))))
-    return replaceInstUsesWith(
-        SI, Builder.CreateMaskedLoad(
+                                           m_Specific(CondVal), m_Value())))){
+
+    llvm::IntrinsicInst *OldLoad = cast<IntrinsicInst>(TrueVal);
+    Instruction* In = Builder.CreateMaskedLoad(
                 TrueVal->getType(), MaskedLoadPtr,
-                cast<IntrinsicInst>(TrueVal)->getParamAlign(0).valueOrOne(),
-                CondVal, FalseVal));
+                OldLoad->getParamAlign(0).valueOrOne(),
+                CondVal, FalseVal);
+    In->copyMetadata(*OldLoad);
----------------
dtcxzyw wrote:

`dropUBImplyingAttrsAndMetadata` removes `!noalias`. So it is unsuitable. Can you use `dropUnknownNonDebugMetadata` to preserve metadata you are interested in?
https://github.com/llvm/llvm-project/blob/3fd07e32350c4f15a9377466ab4cffb042a1ecb7/llvm/lib/Analysis/VectorUtils.cpp#L1062-L1066

All metadata listed in `getMetadataToPropagate` should be safe to propagate. But I am not sure if it is better to duplicate the list here rather than reusing the helper function.


https://github.com/llvm/llvm-project/pull/211965


More information about the llvm-commits mailing list