https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82188
Bug ID: 82188 Summary: Missed optimization opportunity for constant folding Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: ishiura-compiler at ml dot kwansei.ac.jp Target Milestone: --- We compiled two programs (A1.c, A2.c) by GCC-8.0.0 with -O3 option. Though the two programs are equivalent, GCC outputs different assembly codes. +-------------------------------------+---------------------------------+ | A1.c | A2.c | +-------------------------------------+---------------------------------+ |int main (void) |int main (void) | |{ |{ | | volatile int x = 1; | volatile int x = 1; | | | | | long t1 = 1/(2U-x); | long t1 = 1/(2U-x); | | int t2 = 0!=(((int)(2%(2+t1)))/2); | int t2 = 0!=(int)t1; | | | | | if (t1 != 1) __builtin_abort(); | if (t1 != 1) __builtin_abort();| | if (t2 != 1) __builtin_abort(); | if (t2 != 1) __builtin_abort();| | | | | return 0; | return 0; | |} |} | +-------------------------------------+---------------------------------+ +-------------------------------------+-------------------------------------+ | A1.s (gcc-8.0.0 A1.c -O3 -S) | A2.s (gcc-8.0.0 A2.c -O3 -S) | +-------------------------------------+-------------------------------------+ |main: |main: | |.LFB0: |.LFB0: | | .cfi_startproc | .cfi_startproc | | subq $24, %rsp | subq $24, %rsp | | .cfi_def_cfa_offset 32 | .cfi_def_cfa_offset 32 | | movl $2, %esi | movl $2, %ecx | | xorl %edx, %edx | xorl %edx, %edx | | movl $1, 12(%rsp) | movl $1, 12(%rsp) | | movl 12(%rsp), %eax | movl 12(%rsp), %eax | | subl %eax, %esi | subl %eax, %ecx | | movl $1, %eax | movl $1, %eax | | divl %esi | divl %ecx | | movl %eax, %esi | testl %eax, %eax | | movl $2, %eax | je .L3 | | movq %rsi, %rcx | | | cqto | | | addq $2, %rsi | | | idivq %rsi | | | cmpl $1, %ecx | | | jne .L3 | | | cmpq $2, %rdx | | | jne .L3 | | | xorl %eax, %eax | xorl %eax, %eax | | addq $24, %rsp | addq $24, %rsp | | .cfi_def_cfa_offset 8 | .cfi_def_cfa_offset 8 | | ret | ret | | .cfi_endproc | .cfi_endproc | | .section .text.unlikely | .section .text.unlikely | | .cfi_startproc | .cfi_startproc | | .type main.cold.0, @function | .type main.cold.0, @function | |main.cold.0: |main.cold.0: | |.L3: |.L3: | | .cfi_def_cfa_offset 32 | .cfi_def_cfa_offset 32 | | call abort | call abort | | .cfi_endproc | .cfi_endproc | |.LFE0: |.LFE0: | | .section .text.startup | .section .text.startup | | .size main, .-main | .size main, .-main | | .section .text.unlikely | .section .text.unlikely | | .size main.cold.0, .-main.cold.0 | .size main.cold.0, .-main.cold.0 | +-------------------------------------+-------------------------------------+ FYI, LLVM/clang generates the same same assembly codes for A1.c and A2.c. +---------------------------------+---------------------------------+ | A1.s (clang-6.0.0 A1.c -O3 -S) | A2.s (clang-6.0.0 A2.c -O3 -S) | +---------------------------------+---------------------------------+ |main: # @main |main: # @main | | .cfi_startproc | .cfi_startproc | |# BB#0: |# BB#0: | | pushq %rax | pushq %rax | |.Lcfi0: |.Lcfi0: | | .cfi_def_cfa_offset 16 | .cfi_def_cfa_offset 16 | | movl $1, 4(%rsp) | movl $1, 4(%rsp) | | movl 4(%rsp), %eax | movl 4(%rsp), %eax | | cmpl $1, %eax | cmpl $1, %eax | | jne .LBB0_2 | jne .LBB0_2 | |# BB#1: | # BB#1: | | xorl %eax, %eax | xorl %eax, %eax | | popq %rcx | popq %rcx | | retq | retq | |.LBB0_2: |.LBB0_2: | | callq abort | callq abort | |.Lfunc_end0: |.Lfunc_end0: | | .size main, .Lfunc_end0-main| .size main, .Lfunc_end0-main| +---------------------------------+---------------------------------+ gcc-8.0 (GCC) 8.0.0 20170828 (experimental) Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. clang version 6.0.0 (trunk 308582) (llvm/trunk 308581) Target: x86_64-unknown-linux-gnu Thread model: posix