[llvm] [InstCombine] Fix profile propagation in xor-and-or.ll (PR #228246)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 14:51:58 PDT 2026


================
@@ -5748,17 +5748,27 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
       match(&I, m_c_Xor(m_OneUse(m_LogicalAnd(m_Value(A), m_Value(B))),
                         m_OneUse(m_LogicalOr(m_Value(C), m_Value(D)))))) {
     bool NeedFreeze = isa<SelectInst>(Op0) && isa<SelectInst>(Op1) && B == D;
-    Instruction *MDFrom = cast<Instruction>(Op0);
     if (B == C || B == D) {
       std::swap(A, B);
-      MDFrom = B == C ? cast<Instruction>(Op1) : nullptr;
     }
     if (A == C)
       std::swap(C, D);
     if (A == D) {
       if (NeedFreeze)
         A = Builder.CreateFreeze(A);
       Value *NotB = Builder.CreateNot(B);
+      Instruction *MDFrom = nullptr;
+      // If one of the operands has the same condition as we will use for the
+      // select we are going to create, pull the metadata from it (primarily the
+      // profile info).
+      if (auto *Op0SI = dyn_cast<SelectInst>(Op0)) {
+        if (Op0SI->getCondition() == A)
+          MDFrom = Op0SI;
+      }
+      if (auto *Op1SI = dyn_cast<SelectInst>(Op1)) {
+        if (Op1SI->getCondition() == A)
+          MDFrom = Op1SI;
+      }
       return MDFrom == nullptr
----------------
mtrofin wrote:

ablation study

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


More information about the llvm-commits mailing list