[llvm] [CodeGen] Fold load into LZCNT/TZCNT/POPCNT after compare elimination in peephole optimizer (PR #194662)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed May 6 23:16:35 PDT 2026


================
@@ -1792,13 +1823,35 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
         NAPhysToVirtMIs.clear();
       }
 
+      // SrcReg must be captured before MI is erased inside optimizeCmpInstr.
+      Register CmpSrcReg;
+      if (MI->isCompare()) {
+        Register SrcReg2;
+        int64_t CmpMask, CmpValue;
+        if (!TII->analyzeCompare(*MI, CmpSrcReg, SrcReg2, CmpMask, CmpValue) ||
+            CmpSrcReg.isPhysical() || SrcReg2.isPhysical())
+          CmpSrcReg = Register();
+      }
+
       if ((isUncoalescableCopy(*MI) &&
            optimizeUncoalescableCopy(*MI, LocalMIs)) ||
           (MI->isCompare() && optimizeCmpInstr(*MI)) ||
           (MI->isSelect() && optimizeSelect(*MI, LocalMIs))) {
         // MI is deleted.
         LocalMIs.erase(MI);
         Changed = true;
+
+        // The eliminated compare may have been the extra use blocking load
+        // folding into the EFLAGS producer.
----------------
topperc wrote:

EFLAGS is a X86 term. We shouldn't use that in generic code.

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


More information about the llvm-commits mailing list