Issue 60967
Summary local static variable dropped in one but preserved in another equivalent code
Labels question, llvm:optimizations
Assignees
Reporter hiraditya
    The `start_bar` points to a specific section. The code is reduced from u-boot's linker-list macro: https://github.com/u-boot/u-boot/blob/master/include/linker_lists.h#L127

```c
#include<stdio.h>

char* bar() {
    static char start_bar[0] __attribute__((aligned(16)))
		__attribute__((unused))
		__attribute__((section("__u_boot_list_2_1")));
	char *p = (char *)start_bar;
    for (int i = p[0]; i < p[9]; i++)
 printf("asdfasd");
    return 0;
}


char* foo() {
 static char start_foo[0] __attribute__((aligned(16)))
		__attribute__((unused))
		__attribute__((section("__u_boot_list_2_1")));
	char *p = (char *)start_foo;
    for (int i = p[0]; i < p[9] + 10; i++)
 printf("asdfasd");
    return 0;
}

```

$ clang -O2 -fno-unroll-loops

```asm
bar:                                    # @bar
        xor     eax, eax
        ret

foo: # @foo
        push    rbp
        push    rbx
 push    rax
        mov     ebp, 10
        lea     rbx, [rip + .L.str]
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
        mov     rdi, rbx
        xor     eax, eax
 call    printf@PLT
        dec     ebp
        jne     .LBB1_1
 xor     eax, eax
        add     rsp, 8
        pop     rbx
 pop     rbp
        ret
.L.str:
        .asciz "asdfasd"
```

While gcc preserves the object in both cases

$ gcc -O2 -fno-unroll-loops

```asm
.LC0:
 .string "asdfasd"
bar:
        push    rbx
        movsx   eax, BYTE PTR start_bar.1[rip+9]
        movsx   ebx, BYTE PTR start_bar.1[rip]
        cmp     ebx, eax
        jge .L2
.L3:
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        add     ebx, 1
        call    printf
        movsx eax, BYTE PTR start_bar.1[rip+9]
        cmp     eax, ebx
        jg .L3
.L2:
        xor     eax, eax
        pop     rbx
 ret
foo:
        movsx   eax, BYTE PTR start_foo.0[rip+9]
 push    rbx
        movsx   ebx, BYTE PTR start_foo.0[rip]
        add eax, 9
        cmp     ebx, eax
        jg      .L8
.L9:
 mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        add ebx, 1
        call    printf
        movsx   eax, BYTE PTR start_foo.0[rip+9]
        add     eax, 9
        cmp     eax, ebx
 jge     .L9
.L8:
        xor     eax, eax
        pop rbx
        ret
start_foo.0:
start_bar.1:
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to