[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:22:11 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)) ||
----------------
topperc wrote:

Should we move the compare handling out of this compound if so we don't run the new code for the other 2 cases? That might also allow better refactoring so we don't call analyzeCompare inside in this function and inside optimizeCmpInstr.

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


More information about the llvm-commits mailing list