<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60967>60967</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
local static variable dropped in one but preserved in another equivalent code
</td>
</tr>
<tr>
<th>Labels</th>
<td>
question,
llvm:optimizations
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hiraditya
</td>
</tr>
</table>
<pre>
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:
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcV0-P4rgT_TTmUiJKykCSAwdoGv1-EtKMdpFWe0JObIh7TZyxndbMfPqVnYR_zQzTLc1lEep0XCn71XuvYsyslYdaiDmZLsl0NWKtq7SZV9IwLt03Nio0_zbfVgLILLaOGbcrmCGzGBota2fBaWBgG1HKvSzBitJJXUfgM0rNBUgLRvC2FBz2Rh-hHRdaO4KpBSXrf4QZK2kdHFlpNKELqJxrLKELgmuC64N0VVtEpT4SXA-p538KpQuC6yOzThiCa1mXquWC4Lqbe-fntlFFkG4STEm8IvGi_zuLu2_Z3yMdsumTdVzqqCL0-TKlrJghuABPAGYEcyDpsgsBAFjHnCzBPwVnpqbLmExXsNsx54wsWid2u5CdMeWZ5wSzZEYw77_9ejmJ8zs5bd1an_ILj_ZShBvc7dqd5ywQssNdQhDPa9Llaa6AnuCiAUJXQDAbBgjm56LoRdl77eOZrB3IkNT0NRO6DCNPYSQfRgguw7fHD42Rtdt3MJnle2Z5D-5yFSNca2qIz1jT1ZWcVwrttX6j0Ft5_FP_OXl8UR-UBwguIYl_p0pD010N4gRKxeoDjD8hjPe1Hre10UqNldaNvZvP7LEbCW4Maz_6EKRAJrFPOOH1n6_ahKtgXwk-hctV3Ah3CSHwuxhm83dXTzetrUJW0fwoMMx_Grld8ahfO0RF4xEl8XVYCQbDTPgEZLo0sgnaRZvIOuO1DQnRZrlMdskv8BOqoStCn7eVtPD_uhYGNlo38D_BuAgUr0TjKkJXyX2whkuP5lzeI3pLplQgpnPWJP682V6nclGeeLgKvNQiXIcCu-AjIRnnHVI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