llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
25.95k stars 10.59k forks source link

Missed optimization for dead store of 'v1': v1=v2; result=arr[0]; v1=1; #90457

Open ZY546 opened 2 weeks ago

ZY546 commented 2 weeks ago

reduced code: https://godbolt.org/z/b8TPh9xjn

extern signed char v1;
signed char v2; 
unsigned short result;

void func(unsigned short arr [20]) {
    v1 = v2;
    result = arr [0]; 
    v1 = 1;
}

Clang -O3:

define dso_local void @func(unsigned short*)(ptr nocapture noundef readonly %arr) local_unnamed_addr {
entry:
  tail call void @llvm.dbg.value(metadata ptr %arr, metadata !23, metadata !DIExpression())
  %0 = load i8, ptr @v2, align 1
  store i8 %0, ptr @v1, align 1
  %1 = load i16, ptr %arr, align 2
  store i16 %1, ptr @result, align 2
  store i8 1, ptr @v1, align 1
  ret void
}
func(unsigned short*):                              # @func(unsigned short*)
        movzx   eax, byte ptr [rip + v2]
        mov     rcx, qword ptr [rip + v1@GOTPCREL]
        mov     byte ptr [rcx], al
        movzx   eax, word ptr [rdi]
        mov     word ptr [rip + result], ax
        mov     byte ptr [rcx], 1
        ret

Expected code:

func(unsigned short*):
        movzx   eax, WORD PTR [rdi]
        mov     BYTE PTR v1[rip], 1
        mov     WORD PTR result[rip], ax
        ret
XChy commented 2 weeks ago

Looks like clang doesn't emit TBAA information of the external variable v1.