public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/102495] New: optimize some consecutive byte load pattern to word load
@ 2021-09-27  5:31 mytbk920423 at gmail dot com
  2021-09-27  8:48 ` [Bug tree-optimization/102495] " rguenth at gcc dot gnu.org
  2021-12-15 23:29 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: mytbk920423 at gmail dot com @ 2021-09-27  5:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102495
           Summary: optimize some consecutive byte load pattern to word
                    load
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mytbk920423 at gmail dot com
  Target Milestone: ---

I use the following code get a 32-bit word from a byte array by loading each
byte and shifting them, but GCC doesn't optimize the code to a single word load
when I put the byte load in a loop.

Clang trunk can optimize all of the follows:
https://gcc.godbolt.org/z/KfWE67K5c


```
#define SHL(a,b) ((uint32_t)(a) << (b))

// both GCC and Clang optimize to *(uint32_t*)(vv)
uint32_t getword_b(const uint8_t *vv)
{
        return SHL(vv[3], 24) | SHL(vv[2], 16) | SHL(vv[1], 8) | SHL(vv[0], 0);
}

// GCC cannot optimize this, Clang can
uint32_t getword_forloop(const uint8_t *vv)
{
        uint32_t res = 0;
        for (size_t i = 0; i < 4; i++) {
                res |= SHL(vv[i], (i * 8));
        }
        return res;
}

// both GCC and Clang optimize to ((uint32_t*)(vec))[word_idx]
uint32_t getword_from_vec(const uint8_t *vec, size_t word_idx)
{
        size_t byte_idx = word_idx * 4;
        const uint8_t *vv = vec + byte_idx;
        return SHL(vv[3], 24) | SHL(vv[2], 16) | SHL(vv[1], 8) | SHL(vv[0], 0);
}

// neither GCC nor Clang 12.0.1 can optimize this, Clang trunk can
uint32_t getword_from_vec_forloop(const uint8_t *vec, size_t word_idx)
{
        size_t byte_idx = word_idx * 4;
        uint32_t res = 0;
        for (size_t i = 0; i < 4; i++) {
                res |= SHL(vec[byte_idx + i], (i * 8));
        }
        return res;
}
```

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/102495] optimize some consecutive byte load pattern to word load
  2021-09-27  5:31 [Bug other/102495] New: optimize some consecutive byte load pattern to word load mytbk920423 at gmail dot com
@ 2021-09-27  8:48 ` rguenth at gcc dot gnu.org
  2021-12-15 23:29 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-09-27  8:48 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|other                       |tree-optimization
           Keywords|                            |missed-optimization

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
There are related bugreports for the bswap pass.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/102495] optimize some consecutive byte load pattern to word load
  2021-09-27  5:31 [Bug other/102495] New: optimize some consecutive byte load pattern to word load mytbk920423 at gmail dot com
  2021-09-27  8:48 ` [Bug tree-optimization/102495] " rguenth at gcc dot gnu.org
@ 2021-12-15 23:29 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-15 23:29 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2021-12-15
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, this might be a dup.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-15 23:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27  5:31 [Bug other/102495] New: optimize some consecutive byte load pattern to word load mytbk920423 at gmail dot com
2021-09-27  8:48 ` [Bug tree-optimization/102495] " rguenth at gcc dot gnu.org
2021-12-15 23:29 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).