diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp --- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -5446,6 +5446,9 @@ ConstantSDNode *C = dyn_cast(N0.getOperand(1)); if (!C) break; uint64_t Mask = C->getZExtValue(); + // We may have looked through a truncate so mask off any bits that + // shouldn't be part of the compare. + Mask &= maskTrailingOnes(CmpVT.getScalarSizeInBits()); // Check if we can replace AND+IMM64 with a shift. This is possible for // masks/ like 0xFF000000 or 0x00FFFFFF and if we care only about the zero diff --git a/llvm/test/CodeGen/X86/pr51175.ll b/llvm/test/CodeGen/X86/pr51175.ll --- a/llvm/test/CodeGen/X86/pr51175.ll +++ b/llvm/test/CodeGen/X86/pr51175.ll @@ -15,7 +15,7 @@ ; CHECK-NEXT: andl $65527, %eax # imm = 0xFFF7 ; CHECK-NEXT: movl %eax, (%rdx) ; CHECK-NEXT: xorl %eax, %eax -; CHECK-NEXT: testl $-9, %edi +; CHECK-NEXT: testb $-9, %dil ; CHECK-NEXT: cmovel %esi, %eax ; CHECK-NEXT: retq %4 = add i16 %0, 1