Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Simple intraprocedural DSE #50930

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR51963
Status NEW
Importance P enhancement
Reported by David Bolvansky (david.bolvansky@gmail.com)
Reported on 2021-09-24 15:13:20 -0700
Last modified on 2021-09-25 09:20:39 -0700
Version trunk
Hardware PC Linux
CC florian_hahn@apple.com, johannes@jdoerfert.de, llvm-bugs@lists.llvm.org, nikita.ppv@gmail.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
__attribute__ ((noinline))
void
copy (int *a, int *b)
{
  *a=*b;
}

void foo(int b)
{
  int a = 42;
  copy (&a,&b);
}

foo:                                    # @foo
        push    rax
        mov     dword ptr [rsp + 4], edi
        mov     dword ptr [rsp], 42          <------- Useless store
        mov     rdi, rsp
        lea     rsi, [rsp + 4]
        call    copy
        pop     rax
        ret

int p,q;
__attribute__ ((noinline, noclone))

int
test (int *p)
{
  *p = 1;
}
int
test1()
{
  q = 123;
  test(&p);
  return q;
}

test1:                                  # @test1
        push    rax
        mov     dword ptr [rip + q], 123
        mov     edi, offset p
        call    test
        mov     eax, dword ptr [rip + q] // Can be mov     eax, 123
        pop     rcx
        ret
Quuxplusone commented 3 years ago
struct a
{
  int b;
  int c;
};

__attribute__ ((noclone, noinline))
void
test (struct a *a)
{
  a->b = 0;
}

int
foo ()
{
  struct a st = {1,2};
  test (&st);
  return st.c;
}

foo:                                    # @foo
        push    rax
        movabs  rax, 8589934593
        mov     qword ptr [rsp], rax
        mov     rdi, rsp
        call    test
        mov     eax, dword ptr [rsp + 4] // Can be mov     eax, 2
        pop     rcx
        ret
Quuxplusone commented 3 years ago

One way to catch some of the easier cases would be to add a new attribute to indicate that the function writes a certain number of bytes for pointer arguments and/or globals + an analysis that adds the attribute.

It might also be useful to expose it via an attribute in C/C++.