llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Simple intraprocedural DSE

Open davidbolvansky opened this issue 5 years ago • 3 comments

Bugzilla Link 51963
Version trunk
OS Linux
CC @fhahn,@jdoerfert,@nikic

Extended Description

__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

davidbolvansky avatar Sep 24 '21 22:09 davidbolvansky

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++.

fhahn avatar Sep 25 '21 16:09 fhahn

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

davidbolvansky avatar Sep 24 '21 22:09 davidbolvansky

There is a proposal and a prototype for the first case at https://discourse.llvm.org/t/rfc-llvm-new-initialized-parameter-attribute-for-improved-interprocedural-dse/77337

jvoung avatar Mar 08 '24 15:03 jvoung