public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103815] New: Misoptimization of a bounded do/while loop
@ 2021-12-23 13:16 matthias at urlichs dot de
  2021-12-23 19:08 ` [Bug c/103815] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: matthias at urlichs dot de @ 2021-12-23 13:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103815
           Summary: Misoptimization of a bounded do/while loop
           Product: gcc
           Version: og10 (devel/omp/gcc-10)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matthias at urlichs dot de
  Target Milestone: ---

This code, which calculates an integer square root ..:

    #include <inttypes.h>
    uint16_t int_sqrt32(uint32_t x)
    {
        uint16_t res=0;
        uint16_t add= 0x8000;   
        do {
            uint16_t temp=res | add;
            uint32_t g2=temp*temp;      
            if (x>=g2)
                res=temp;           
            add>>=1;
        } while(add);
        return res;
    }

... should be compileable 1:1, since the right shift sets the condition flags
appropriately.

Unfortunately, GCC's optimizer notices that this is a 16-step loop, "helpfully"
invents a loop counter, and pessimizes the code to this sub-optimal result (ARM
Thumb output; x86 has essentially the same problem):

   0:   b500            push    {lr}
   2:   2110            movs    r1, #16
   4:   4686            mov     lr, r0
   6:   f44f 4200       mov.w   r2, #32768      ; 0x8000
   a:   2000            movs    r0, #0
   c:   ea40 0302       orr.w   r3, r0, r2
  10:   0852            lsrs    r2, r2, #1
  12:   b29b            uxth    r3, r3
  14:   fb03 fc03       mul.w   ip, r3, r3
  18:   45f4            cmp     ip, lr
  1a:   bf98            it      ls
  1c:   4618            movls   r0, r3
  1e:   3901            subs    r1, #1
  20:   d1f4            bne.n   c <int_sqrt32+0xc>
  22:   f85d fb04       ldr.w   pc, [sp], #4

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

* [Bug c/103815] Misoptimization of a bounded do/while loop
  2021-12-23 13:16 [Bug c/103815] New: Misoptimization of a bounded do/while loop matthias at urlichs dot de
@ 2021-12-23 19:08 ` pinskia at gcc dot gnu.org
  2021-12-27 22:09 ` [Bug tree-optimization/103815] IVCann/IVOPTs changes induction variable so it is an addition but the need for shift is there and the result could have used for the (loop) exit compare pinskia at gcc dot gnu.org
  2022-01-04 13:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-23 19:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note temp*temp is really ((int)temp)*((int)temp) due to interger promotion
rules in c/c++.

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

* [Bug tree-optimization/103815] IVCann/IVOPTs changes induction variable so it is an addition but the need for shift is there and the result could have used for the (loop) exit compare
  2021-12-23 13:16 [Bug c/103815] New: Misoptimization of a bounded do/while loop matthias at urlichs dot de
  2021-12-23 19:08 ` [Bug c/103815] " pinskia at gcc dot gnu.org
@ 2021-12-27 22:09 ` pinskia at gcc dot gnu.org
  2022-01-04 13:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-27 22:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug tree-optimization/103815] IVCann/IVOPTs changes induction variable so it is an addition but the need for shift is there and the result could have used for the (loop) exit compare
  2021-12-23 13:16 [Bug c/103815] New: Misoptimization of a bounded do/while loop matthias at urlichs dot de
  2021-12-23 19:08 ` [Bug c/103815] " pinskia at gcc dot gnu.org
  2021-12-27 22:09 ` [Bug tree-optimization/103815] IVCann/IVOPTs changes induction variable so it is an addition but the need for shift is there and the result could have used for the (loop) exit compare pinskia at gcc dot gnu.org
@ 2022-01-04 13:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-04 13:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-01-04
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's difficult for IVOPTs to handle this since CC are not modeled on GIMPLE.
'add' is also not an affine induction variable and thus is not considered
at all when representing the exit test.

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

end of thread, other threads:[~2022-01-04 13:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-23 13:16 [Bug c/103815] New: Misoptimization of a bounded do/while loop matthias at urlichs dot de
2021-12-23 19:08 ` [Bug c/103815] " pinskia at gcc dot gnu.org
2021-12-27 22:09 ` [Bug tree-optimization/103815] IVCann/IVOPTs changes induction variable so it is an addition but the need for shift is there and the result could have used for the (loop) exit compare pinskia at gcc dot gnu.org
2022-01-04 13:24 ` rguenth 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).