https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67809

            Bug ID: 67809
           Summary: Empty pointer-chasing loops aren't optimized out
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matt at godbolt dot org
  Target Milestone: ---

The following code:

```
struct Foo {
  Foo *next; 

  void release() {
    Foo *tmp = 0;
    for (Foo *it = next; it; it = tmp) {
      tmp = it->next;
    }
  }
};

void test(Foo &f) { f.release(); }
```

Results in pointer-chasing code when compiled at -O3:

```
test(Foo&):
        mov     rax, QWORD PTR [rdi]
        test    rax, rax
        je      .L1
.L3:
        mov     rax, QWORD PTR [rax]
        test    rax, rax
        jne     .L3
.L1:
        rep ret
```

clang and icc both optimize this to a single ret. e.g. https://goo.gl/saN4XC vs
https://goo.gl/LeUGn0

Would be nice for this loop to go away completely. For context, this was in
code I added to use ASAN_POISON_MEMORY_REGION() around a pooled allocator upon
free of a list (each node was poisoned individually). Without
-fsanitize=address I was expecting the loop to entirely vanish, but the
pointer-chase was still done.

Reply via email to