Index: llvm/lib/Target/X86/X86ISelDAGToDAG.cpp =================================================================== --- llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ llvm/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -369,6 +369,12 @@ (isOneConstant(SDValue(N, 0)) || isAllOnesConstant(SDValue(N, 0)))) continue; + // If this is a sign-extended 8-bit integer immediate used in an ALU + // instruction, there is probably an opcode encoding to save space. + auto *C = dyn_cast(N); + if (C && isInt<8>(C->getSExtValue())) + continue; + // Immediates that are used for offsets as part of stack // manipulation should be left alone. These are typically // used to indicate SP offsets for argument passing and Index: llvm/test/CodeGen/X86/immediate_merging.ll =================================================================== --- llvm/test/CodeGen/X86/immediate_merging.ll +++ llvm/test/CodeGen/X86/immediate_merging.ll @@ -19,9 +19,8 @@ ; X86-NEXT: movl $1234, %eax # imm = 0x4D2 ; X86-NEXT: movl %eax, a ; X86-NEXT: movl %eax, b -; X86-NEXT: movl $12, %eax -; X86-NEXT: movl %eax, c -; X86-NEXT: cmpl %eax, e +; X86-NEXT: movl $12, c +; X86-NEXT: cmpl $12, e ; X86-NEXT: jne .LBB0_2 ; X86-NEXT: # %bb.1: # %if.then ; X86-NEXT: movl $1, x @@ -38,9 +37,8 @@ ; X64-NEXT: movl $1234, %eax # imm = 0x4D2 ; X64-NEXT: movl %eax, {{.*}}(%rip) ; X64-NEXT: movl %eax, {{.*}}(%rip) -; X64-NEXT: movl $12, %eax -; X64-NEXT: movl %eax, {{.*}}(%rip) -; X64-NEXT: cmpl %eax, {{.*}}(%rip) +; X64-NEXT: movl $12, {{.*}}(%rip) +; X64-NEXT: cmpl $12, {{.*}}(%rip) ; X64-NEXT: jne .LBB0_2 ; X64-NEXT: # %bb.1: # %if.then ; X64-NEXT: movl $1, {{.*}}(%rip) @@ -81,9 +79,8 @@ ; X86-NEXT: movl $1234, %eax # imm = 0x4D2 ; X86-NEXT: movl %eax, a ; X86-NEXT: movl %eax, b -; X86-NEXT: movl $12, %eax -; X86-NEXT: movl %eax, c -; X86-NEXT: cmpl %eax, e +; X86-NEXT: movl $12, c +; X86-NEXT: cmpl $12, e ; X86-NEXT: jne .LBB1_2 ; X86-NEXT: # %bb.1: # %if.then ; X86-NEXT: movl $1, x @@ -100,9 +97,8 @@ ; X64-NEXT: movl $1234, %eax # imm = 0x4D2 ; X64-NEXT: movl %eax, {{.*}}(%rip) ; X64-NEXT: movl %eax, {{.*}}(%rip) -; X64-NEXT: movl $12, %eax -; X64-NEXT: movl %eax, {{.*}}(%rip) -; X64-NEXT: cmpl %eax, {{.*}}(%rip) +; X64-NEXT: movl $12, {{.*}}(%rip) +; X64-NEXT: cmpl $12, {{.*}}(%rip) ; X64-NEXT: jne .LBB1_2 ; X64-NEXT: # %bb.1: # %if.then ; X64-NEXT: movl $1, {{.*}}(%rip) Index: llvm/test/CodeGen/X86/immediate_merging64.ll =================================================================== --- llvm/test/CodeGen/X86/immediate_merging64.ll +++ llvm/test/CodeGen/X86/immediate_merging64.ll @@ -9,9 +9,8 @@ define i1 @imm_multiple_users(i64 %a, i64* %b) optsize { ; CHECK-LABEL: imm_multiple_users: ; CHECK: # %bb.0: -; CHECK-NEXT: movq $-1, %rax -; CHECK-NEXT: movq %rax, (%rsi) -; CHECK-NEXT: cmpq %rax, %rdi +; CHECK-NEXT: movq $-1, (%rsi) +; CHECK-NEXT: cmpq $-1, %rdi ; CHECK-NEXT: sete %al ; CHECK-NEXT: retq store i64 -1, i64* %b, align 8 @@ -22,9 +21,8 @@ define i1 @imm_multiple_users_pgso(i64 %a, i64* %b) !prof !14 { ; CHECK-LABEL: imm_multiple_users_pgso: ; CHECK: # %bb.0: -; CHECK-NEXT: movq $-1, %rax -; CHECK-NEXT: movq %rax, (%rsi) -; CHECK-NEXT: cmpq %rax, %rdi +; CHECK-NEXT: movq $-1, (%rsi) +; CHECK-NEXT: cmpq $-1, %rdi ; CHECK-NEXT: sete %al ; CHECK-NEXT: retq store i64 -1, i64* %b, align 8 Index: llvm/test/CodeGen/X86/pr27202.ll =================================================================== --- llvm/test/CodeGen/X86/pr27202.ll +++ llvm/test/CodeGen/X86/pr27202.ll @@ -17,9 +17,8 @@ define zeroext i1 @g(i32 %x) optsize { ; CHECK-LABEL: g: ; CHECK: # %bb.0: -; CHECK-NEXT: movl $1, %eax -; CHECK-NEXT: orl %eax, %edi -; CHECK-NEXT: cmpl %eax, %edi +; CHECK-NEXT: orl $1, %edi +; CHECK-NEXT: cmpl $1, %edi ; CHECK-NEXT: sete %al ; CHECK-NEXT: retq %t0 = or i32 %x, 1 @@ -36,9 +35,8 @@ ; CHECK-NEXT: movl %edx, %eax ; CHECK-NEXT: shll $6, %eax ; CHECK-NEXT: movzbl %al, %ecx -; CHECK-NEXT: movl $7, %eax -; CHECK-NEXT: andq %rax, %rsi -; CHECK-NEXT: andq %rax, %rdx +; CHECK-NEXT: andl $7, %esi +; CHECK-NEXT: andl $7, %edx ; CHECK-NEXT: leaq (%rdx,%rsi,8), %rax ; CHECK-NEXT: orq %rcx, %rax ; CHECK-NEXT: retq