diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp --- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -18760,7 +18760,7 @@ SDNode *SubsNode = N->getOperand(CmpIndex).getNode(); unsigned CondOpcode = SubsNode->getOpcode(); - if (CondOpcode != AArch64ISD::SUBS) + if (CondOpcode != AArch64ISD::SUBS || SubsNode->hasAnyUseOfValue(0)) return SDValue(); // There is a SUBS feeding this condition. Is it fed by a mask we can diff --git a/llvm/test/CodeGen/AArch64/and-mask-removal.ll b/llvm/test/CodeGen/AArch64/and-mask-removal.ll --- a/llvm/test/CodeGen/AArch64/and-mask-removal.ll +++ b/llvm/test/CodeGen/AArch64/and-mask-removal.ll @@ -486,6 +486,7 @@ ; CHECK-SD-LABEL: pr58109: ; CHECK-SD: ; %bb.0: ; CHECK-SD-NEXT: add w8, w0, #1 +; CHECK-SD-NEXT: and w8, w8, #0xff ; CHECK-SD-NEXT: subs w8, w8, #1 ; CHECK-SD-NEXT: csel w0, wzr, w8, lo ; CHECK-SD-NEXT: ret