[Bug c/103815] New: Misoptimization of a bounded do/while loop

2021-12-23 Thread matthias at urlichs dot de via 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 
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:   b500push{lr}
   2:   2110movsr1, #16
   4:   4686mov lr, r0
   6:   f44f 4200   mov.w   r2, #32768  ; 0x8000
   a:   2000movsr0, #0
   c:   ea40 0302   orr.w   r3, r0, r2
  10:   0852lsrsr2, r2, #1
  12:   b29buxthr3, r3
  14:   fb03 fc03   mul.w   ip, r3, r3
  18:   45f4cmp ip, lr
  1a:   bf98it  ls
  1c:   4618movls   r0, r3
  1e:   3901subsr1, #1
  20:   d1f4bne.n   c 
  22:   f85d fb04   ldr.w   pc, [sp], #4

[Bug c++/104081] New: Variable optimized out despite -Og

2022-01-18 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104081

Bug ID: 104081
   Summary: Variable optimized out despite -Og
   Product: gcc
   Version: 12.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: matthias at urlichs dot de
  Target Milestone: ---

I expect the optimizer, when confronted with "-Og", to keep my variables
debuggable.

(I also expect (of me) to not make terminally stupid mistakes like the one
exhibited here, but that's a different problem …)

// "data" is a std::string_view
(gdb) l
19  if(data.length() == 0)
20  throw_invalid("empty",data);
21  }
22  for (const auto& c : data) {
23  if ('0' < c || '9' > c)
24  throw_invalid("not a number",data);
25  val = val*10 + c-'0';
26  }
27  if(neg)
28  val = -val;
(gdb) p data
$1 = 
(gdb) p c
$2 = 
(gdb) q

Options: g++-12 -march=native -Og -g -xc++ -std=c++17 …

[Bug c++/104081] Variable optimized out despite -Og

2022-01-18 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104081

--- Comment #1 from Matthias Urlichs  ---
current line is 24, after "throw_invalid" has called abort()

[Bug middle-end/104081] Variable optimized out despite -Og

2022-01-18 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104081

--- Comment #3 from Matthias Urlichs  ---
Sure.

gcc -Og -g -std=c++17 -lstdc++ /tmp/test.cpp


#include 
#include 

void throw_invalid(const char *a, std::string_view b) {
(void)a; (void)b;
throw;
}

int64_t str_atoi(std::string_view data)
{
const std::string_view odata = data;

int64_t val = 0;
bool neg;

if(data.length() == 0)
throw_invalid("empty",odata);
neg = data.front() == '-';
if(neg) {
data.remove_prefix(1);
if(data.length() == 0)
throw_invalid("empty",data);
}
for (const auto& c : data) {
if (c < '0' || '9' < c)
throw_invalid("not a number",odata);
val = val*10 + c-'0';
}
if(neg)
val = -val;
return val;
}

int main() {
volatile int x =  str_atoi("nope");
}

[Bug middle-end/104081] Variable optimized out despite -Og

2022-01-18 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104081

--- Comment #5 from Matthias Urlichs  ---
> when I break at line 24 the data is still there but it's lost 
> when unwinding frames from the SIGABRT:

Yes, that's exactly the problem.

[Bug middle-end/104081] Variable optimized out despite -Og

2024-06-14 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104081

Matthias Urlichs  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |DUPLICATE

--- Comment #7 from Matthias Urlichs  ---
@user202729 confirmed. Closing in favor of the earlier issue.

*** This bug has been marked as a duplicate of bug 78685 ***

[Bug debug/78685] -Og generates too many ""s

2024-06-14 Thread matthias at urlichs dot de via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78685

Matthias Urlichs  changed:

   What|Removed |Added

 CC||matthias at urlichs dot de

--- Comment #24 from Matthias Urlichs  ---
*** Bug 104081 has been marked as a duplicate of this bug. ***