public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/55906] New: suboptimal code generated for post-inc on Thumb1
@ 2013-01-08  5:56 amker.cheng at gmail dot com
  2014-03-20 19:21 ` [Bug tree-optimization/55906] " ramana at gcc dot gnu.org
  2023-06-21  4:04 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: amker.cheng at gmail dot com @ 2013-01-08  5:56 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55906

             Bug #: 55906
           Summary: suboptimal code generated for post-inc on Thumb1
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: amker.cheng@gmail.com


For below program:

int
ffs(int word)

{
  int i;

  if (!word)
    return 0;

  i = 0;
  for (;;)
    {
      if (((1 << i++) & word) != 0)
 return i;
    }
}

The dump of 164t.optimized is like:
ffs (int word)
{
  int i;
  int _6;
  int _7;

  <bb 2>:
  if (word_3(D) == 0)
    goto <bb 6>;
  else
    goto <bb 3>;

  <bb 3>:

  <bb 4>:
  # i_1 = PHI <0(3), i_5(5)>
  i_5 = i_1 + 1;
  _6 = word_3(D) >> i_1;
  _7 = _6 & 1;
  if (_7 != 0)
    goto <bb 6>;
  else
    goto <bb 5>;

  <bb 5>:
  goto <bb 4>;

  <bb 6>:
  # i_2 = PHI <0(2), i_5(4)>
  return i_2;

}
GCC increases i before i_1 is used, causing i_5 and i_1 to be partitioned into
different partitions as in expanded rtl:
    2: r115:SI=r0:SI
    3: NOTE_INSN_FUNCTION_BEG
    9: pc={(r115:SI==0)?L33:pc}
      REG_BR_PROB 0xf3c
   10: NOTE_INSN_BASIC_BLOCK 4
    4: r110:SI=0
   18: L18:
   11: NOTE_INSN_BASIC_BLOCK 5
   12: r111:SI=r110:SI+0x1        <-----i_5/i_1 in different pseudos
   13: r116:SI=r115:SI>>r110:SI
   14: r118:SI=0x1
   15: r117:SI=r116:SI&r118:SI
      REG_EQUAL r116:SI&0x1
   16: pc={(r117:SI!=0)?L21:pc}
      REG_BR_PROB 0x384
   17: NOTE_INSN_BASIC_BLOCK 6
    5: r110:SI=r111:SI
   19: pc=L18
   20: barrier
   33: L33:
   32: NOTE_INSN_BASIC_BLOCK 7
    6: r111:SI=0
   21: L21:
   22: NOTE_INSN_BASIC_BLOCK 8
   23: r114:SI=r111:SI
   27: r0:SI=r114:SI
   30: use r0:SI

Finally, suboptimal codes are generated :
ffs:
    mov    r3, #0
    push    {r4, lr}
    cmp    r0, r3
    beq    .L2
    mov    r2, r3
    mov    r1, #1
.L3:
    mov    r4, r0
    asr    r4, r4, r2
    add    r3, r2, #1
    tst    r4, r1
    bne    .L2
    mov    r2, r3
    b    .L3
.L2:
    mov    r0, r3
    @ sp needed
    pop    {r4, pc}

While GCC 4.6 generates better codes:
ffs:
    push    {lr}
    sub    r3, r0, #0
    beq    .L2
    mov    r3, #0
    mov    r2, #1
.L3:
    mov    r1, r0
    asr    r1, r1, r3
    add    r3, r3, #1
    tst    r1, r2
    beq    .L3
.L2:
    mov    r0, r3
    @ sp needed for prologue
    pop    {pc}


The command line is:
arm-none-eabi-gcc -mthumb -mcpu=cortex-m0 -Os -S ffs.c -o ffs.S

Same problem exists when optimizing with "-O2"


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

* [Bug tree-optimization/55906] suboptimal code generated for post-inc on Thumb1
  2013-01-08  5:56 [Bug tree-optimization/55906] New: suboptimal code generated for post-inc on Thumb1 amker.cheng at gmail dot com
@ 2014-03-20 19:21 ` ramana at gcc dot gnu.org
  2023-06-21  4:04 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: ramana at gcc dot gnu.org @ 2014-03-20 19:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55906

Ramana Radhakrishnan <ramana at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2014-03-20
                 CC|                            |ramana at gcc dot gnu.org
     Ever confirmed|0                           |1


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

* [Bug tree-optimization/55906] suboptimal code generated for post-inc on Thumb1
  2013-01-08  5:56 [Bug tree-optimization/55906] New: suboptimal code generated for post-inc on Thumb1 amker.cheng at gmail dot com
  2014-03-20 19:21 ` [Bug tree-optimization/55906] " ramana at gcc dot gnu.org
@ 2023-06-21  4:04 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-21  4:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
      Known to work|                            |10.1.0, 9.1.0
      Known to fail|                            |8.5.0
             Status|NEW                         |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
In GCC 9+:
ffs:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        movs    r3, r0
        movs    r0, #0
        push    {r4, lr}
        cmp     r3, r0
        beq     .L1
        movs    r1, #1
.L3:
        movs    r4, r3
        asrs    r4, r4, r0
        adds    r0, r0, #1
        tst     r4, r1
        beq     .L3
.L1:
        @ sp needed
        pop     {r4, pc}

So no move after inbetween the loop and the return.

GCC 5-8 produces:
ffs:
        movs    r3, #0
        cmp     r0, r3
        beq     .L2
        movs    r2, #1
.L3:
        movs    r1, r0
        asrs    r1, r1, r3
        adds    r3, r3, #1
        tst     r1, r2
        beq     .L3
.L2:
        movs    r0, r3
        @ sp needed
        bx      lr

So no extra move there either but GCC 9+ seems better, though using one extra
register.

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

end of thread, other threads:[~2023-06-21  4:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-08  5:56 [Bug tree-optimization/55906] New: suboptimal code generated for post-inc on Thumb1 amker.cheng at gmail dot com
2014-03-20 19:21 ` [Bug tree-optimization/55906] " ramana at gcc dot gnu.org
2023-06-21  4:04 ` 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).