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

            Bug ID: 128568
           Summary: sh: wrong code for loop
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
            Blocks: 118443
  Target Milestone: ---
            Target: sh-elf

The function foo below is expected to execute two iterations of the loop when
called as
  foo(&buf[5], -5, -1);
The expected output is therefore "1 0 1". But compiling the code with
optimization causes the loop to execute fewer iterations:
 * Compiling with
     sh-elf-gcc -ml -m4-single -O1 bug.c
   makes it print "1 0 0" instead.
 * Compiling with
     sh-elf-gcc -ml -m4-single -Os bug.c
   makes it print "0 0 0".


#include <stdio.h>

__attribute__((noipa)) void
foo (char *p, unsigned int start, unsigned int n)
{
  for (unsigned int i = start; i < n; i += 2)
    {
      char *q = p + (int)i;
      *q = 1;
    }
}

int main()
{
  char buf[5] = {0, 0, 0, 0, 0};
  foo(&buf[5], -5, -1);
  printf("%d %d %d\n", buf[0], buf[1], buf[2]);
  return 0;
}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118443
[Bug 118443] [Meta bug] Bugs triggered by and blocking more smtgcc testing

Reply via email to