https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109343

            Bug ID: 109343
           Summary: invalid if conversion optimization for aarch64
           Product: gcc
           Version: rust/master
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jiangning.liu at amperecomputing dot com
  Target Milestone: ---

For this small case, if-conversion optimization in back-end generated csel
instruction for aarch64, which is unsafe. The address of variable sga_var could
be invalid if sga_mapped is false.

$ cat ttt2.c
extern int sga_mapped, sga_var;
int func(int j){
        int i=0;
        if(sga_mapped)
                i=i+sga_var;
        return i;
}
$ gcc -O3 -S ttt2.c
$ cat ttt2.s
        .arch armv8-a
        .file   "ttt2.c"
        .text
        .align  2
        .p2align 4,,11
        .global func
        .type   func, %function
func:
.LFB0:
        .cfi_startproc
        adrp    x0, sga_mapped
        adrp    x1, sga_var
        ldr     w0, [x0, #:lo12:sga_mapped]
        ldr     w1, [x1, #:lo12:sga_var]
        cmp     w0, 0
        csel    w0, w1, w0, ne
        ret
        .cfi_endproc
.LFE0:
        .size   func, .-func
        .ident  "GCC: (GNU) 12.2.1 20221121 (Red Hat 12.2.1-4)"
        .section        .note.GNU-stack,"",@progbits

For x86, the following code is generated. It is safe because the memory access
to sga_var(%rip) won't be really triggered if %eax is not set. Here x86 and
aarch64 are different.

$ cat ttt2.s
        .file   "ttt2.c"
        .text
        .p2align 4
        .globl  func
        .type   func, @function
func:
.LFB0:
        .cfi_startproc
        endbr64
        movl    sga_mapped(%rip), %eax
        testl   %eax, %eax
        cmovne  sga_var(%rip), %eax
        ret
  • [Bug rtl-optimizatio... jiangning.liu at amperecomputing dot com via Gcc-bugs

Reply via email to