diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp index e9a599f3cfbd..a1989803e9cf 100644 --- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -583,6 +583,12 @@ X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const { Imm->getAPIntValue().getBitWidth() == 64 && Imm->getAPIntValue().isIntN(32)) return false; + + // ADD/SUB with can negate the immediate and use the opposite operation + // to fit 128 into a sign extended 8 bit immediate. + if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB) && + (-Imm->getAPIntValue()).isSignedIntN(8)) + return false; } // If the other operand is a TLS address, we should fold it instead. diff --git a/llvm/test/CodeGen/X86/lack-of-signed-truncation-check.ll b/llvm/test/CodeGen/X86/lack-of-signed-truncation-check.ll index 6509b76bb1d2..e533f7888c4f 100644 --- a/llvm/test/CodeGen/X86/lack-of-signed-truncation-check.ll +++ b/llvm/test/CodeGen/X86/lack-of-signed-truncation-check.ll @@ -485,8 +485,8 @@ define i1 @add_ugecmp_bad_i16_i8_add(i16 %x, i16 %y) nounwind { define i1 @add_ugecmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind { ; X86-LABEL: add_ugecmp_bad_i16_i8_cmp: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: cmpw {{[0-9]+}}(%esp), %ax ; X86-NEXT: setae %al ; X86-NEXT: retl @@ -506,8 +506,8 @@ define i1 @add_ugecmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind { define i1 @add_ugecmp_bad_i8_i16(i16 %x) nounwind { ; X86-LABEL: add_ugecmp_bad_i8_i16: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: cmpw $127, %ax ; X86-NEXT: seta %al ; X86-NEXT: retl @@ -550,8 +550,8 @@ define i1 @add_ugecmp_bad_i16_i8_c0notpoweroftwo(i16 %x) nounwind { define i1 @add_ugecmp_bad_i16_i8_c1notpoweroftwo(i16 %x) nounwind { ; X86-LABEL: add_ugecmp_bad_i16_i8_c1notpoweroftwo: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: movzwl %ax, %eax ; X86-NEXT: cmpl $767, %eax # imm = 0x2FF ; X86-NEXT: seta %al @@ -617,8 +617,8 @@ define i1 @add_ugecmp_bad_i16_i4(i16 %x) nounwind { define i1 @add_ugecmp_bad_i24_i8(i24 %x) nounwind { ; X86-LABEL: add_ugecmp_bad_i24_i8: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: andl $16777215, %eax # imm = 0xFFFFFF ; X86-NEXT: cmpl $255, %eax ; X86-NEXT: seta %al diff --git a/llvm/test/CodeGen/X86/signed-truncation-check.ll b/llvm/test/CodeGen/X86/signed-truncation-check.ll index c455f3c4c8aa..1e55b4db0702 100644 --- a/llvm/test/CodeGen/X86/signed-truncation-check.ll +++ b/llvm/test/CodeGen/X86/signed-truncation-check.ll @@ -485,8 +485,8 @@ define i1 @add_ultcmp_bad_i16_i8_add(i16 %x, i16 %y) nounwind { define i1 @add_ultcmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind { ; X86-LABEL: add_ultcmp_bad_i16_i8_cmp: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: cmpw {{[0-9]+}}(%esp), %ax ; X86-NEXT: setb %al ; X86-NEXT: retl @@ -548,8 +548,8 @@ define i1 @add_ultcmp_bad_i16_i8_c0notpoweroftwo(i16 %x) nounwind { define i1 @add_ultcmp_bad_i16_i8_c1notpoweroftwo(i16 %x) nounwind { ; X86-LABEL: add_ultcmp_bad_i16_i8_c1notpoweroftwo: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: movzwl %ax, %eax ; X86-NEXT: cmpl $768, %eax # imm = 0x300 ; X86-NEXT: setb %al @@ -615,8 +615,8 @@ define i1 @add_ultcmp_bad_i16_i4(i16 %x) nounwind { define i1 @add_ultcmp_bad_i24_i8(i24 %x) nounwind { ; X86-LABEL: add_ultcmp_bad_i24_i8: ; X86: # %bb.0: -; X86-NEXT: movl $128, %eax -; X86-NEXT: addl {{[0-9]+}}(%esp), %eax +; X86-NEXT: movl {{[0-9]+}}(%esp), %eax +; X86-NEXT: subl $-128, %eax ; X86-NEXT: andl $16777215, %eax # imm = 0xFFFFFF ; X86-NEXT: cmpl $256, %eax # imm = 0x100 ; X86-NEXT: setb %al