[llvm] [CodeGenPrepare] Don't sink icmp eq (and X, mask), 0 into return blocks (PR #200460)

Aayush Shrivastava via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 10:48:15 PDT 2026


================
@@ -1911,6 +1911,27 @@ static bool sinkCmpExpression(CmpInst *Cmp, const TargetLowering &TLI,
     if (isa<PHINode>(User))
       continue;
 
+    // Don't sink an "icmp eq (and X, mask), 0" into a return block when the
+    // 'and' currently has a single use in the same block as the cmp. Sinking
+    // would give the 'and' a second use (in the return block), causing
+    // sinkAndCmp0Expression to duplicate the 'and' there too, creating a
+    // redundant TEST+SETCC sequence. The backend's CopyToExportRegsIfNeeded
+    // mechanism handles this cross-block use of the condition without any
+    // extra work.
+    if (isa<ReturnInst>(User)) {
+      if (auto *ICmpI = dyn_cast<ICmpInst>(Cmp)) {
----------------
iamaayushrivastava wrote:

Thanks for the suggestion! Replaced the nested `dyn_cast` chain with `match(Cmp, m_ICmp(m_OneUse(m_And(...)), m_ZeroInt()))` as recommended.

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


More information about the llvm-commits mailing list