[Bug tree-optimization/104632] Missed optimization about reading backwards

lh_mouse at 126 dot com gcc-bugzilla@gcc.gnu.org
Tue Feb 22 09:19:24 GMT 2022


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

--- Comment #2 from LIU Hao <lh_mouse at 126 dot com> ---
I don't think it's a duplicate. This only happens when reading through a
pointer by negative offsets. If I change the code to read by non-negative
offsets, GCC is actually very happy about it:

https://gcc.godbolt.org/z/sT9hzcndW

```
// In real code this was
//   `load_le32_backwards(::std::reverse_iterator<const unsigned char*> ptr)
unsigned
load_le32_backwards(const unsigned char* ptr)
  {
    unsigned word =    ptr[3];
    word = word << 8 | ptr[2];
    word = word << 8 | ptr[1];
    word = word << 8 | ptr[0];
    return word;
  }

```

```
load_le32_backwards(unsigned char const*):
        mov     eax, DWORD PTR [rdi]
        ret
```


More information about the Gcc-bugs mailing list