[Bug c/68462] New: -fno-strict-aliasing not respected

2015-11-20 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68462

Bug ID: 68462
   Summary: -fno-strict-aliasing not respected
   Product: gcc
   Version: 4.8.4
Status: UNCONFIRMED
  Severity: major
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: manjeetdahiya at gmail dot com
  Target Milestone: ---

For the following code -fno-strict-aliasing is not respected in function
add_link_to_domain. The variable N_domains is cached before executing:

  domain_array[N_domains].lol = lol;
  domain_array[N_domains].size++;

However, it should have been read again from the memory for the second
statement. 

* I checked on clang-3.6 and it works as desired.
* Another thing I noticed is that when I make the variable N_domains non-static
-fno-strict-aliasing is respected.

code listing:
-
struct List_o_links{
struct List_o_links * next;
};

struct Domain{
int size;
struct List_o_links * lol;
};

static int N_domains;
int Size;
static struct Domain domain_array[500];

void* mymalloc(int size);

void add_link_to_domain(int link) {
struct List_o_links *lol;
lol = (struct List_o_links *) mymalloc(sizeof(struct List_o_links));
lol->next = domain_array[N_domains].lol;
domain_array[N_domains].lol = lol;
domain_array[N_domains].size++;
}

void build_dom()
{
  int i;
  for(i = 0; i < Size; ++i)
N_domains++;
}

Relevent assembly:
--
N_global static:
  call  mymalloc
  movl  N_domains, %edx
  movl  domain_array+4(,%edx,8), %ecx
  addl  $1, domain_array(,%edx,8)
  movl  %eax, domain_array+4(,%edx,8)

N_global non-static:
  call  mymalloc
  movl  N_domains, %edx
  movl  domain_array+4(,%edx,8), %edx
  movl  %edx, (%eax)
  movl  N_domains, %edx
  movl  %eax, domain_array+4(,%edx,8)

[Bug c/68462] -fno-strict-aliasing not respected

2015-11-20 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68462

--- Comment #1 from Manjeet Dahiya  ---
GCC version details:
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)

Here are the options I used:
gcc -c -O2 -fno-strict-aliasing -m32 -S 

[Bug c/68462] -fno-strict-aliasing not respected

2015-11-20 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68462

--- Comment #3 from Manjeet Dahiya  ---
(In reply to Andrew Pinski from comment #2)
> N_domains and domain_array don't overlap at all and it is known to overlap
> as writing past arrays bounds is undefined and is unrelated to strict
> aliasing.

Thanks for clarification. Can you please explain why GCC doesn't cache
(N_domains) when we make the variable N_domains non-static. Is it the case that
GCC missed the chance to exploit undefined behavior or something else?

[Bug c/68480] New: strict-aliasing not respected

2015-11-22 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68480

Bug ID: 68480
   Summary: strict-aliasing not respected
   Product: gcc
   Version: 4.1.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: manjeetdahiya at gmail dot com
  Target Milestone: ---

When I compile following code with -O2 -m32 -S -fno-strict-aliasing
-fno-unrolling -fno-builtin. GCC seems to be not respecting
-fno-strict-aliasing. I don't encounter this issue in gcc-4.8.

struct list { int hd; struct list * tl; };

struct list * reverselist (struct list * l)
{
  struct list * r, * r2;
  for (r = NULL; l != NULL; l = l->tl) {
r2 = mymalloc(sizeof(struct list));
r2->hd = l->hd;
r2->tl = r;
r = r2;
  }
  return r;
}

The issue is that statement A precedes B. It is fine in gcc-4.8.4.

Assembly gcc-4.1.0:
movl  (%ebx), %eax  # .hd, .hd
movl  %esi, 4(%edx) # r, .tl
movl  %edx, %esi  # D.3244, r
A:  movl  4(%ebx), %ebx # .tl, l
B:  movl  %eax, (%edx)  # .hd, .hd
testl %ebx, %ebx  # l

Assembly gcc-4.8.4:
movl  (%ebx), %edx  # l_16->hd, D.5537
movl  %esi, 4(%eax) # r, r_7->tl
movl  %edx, (%eax)  # D.5537, r_7->hd
movl  4(%ebx), %ebx # l_16->tl, l
testl %ebx, %ebx  # l

[Bug c/68480] -fno-strict-aliasing not respected

2015-11-22 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68480

--- Comment #2 from Manjeet Dahiya  ---
(In reply to Andreas Schwab from comment #1)
> Did you mean gcc-5.1.0?

No. It is for gcc-4.1.0 :) We were checking for equivalence of code generated
across different versions and found this bug. We saw that it is possible to
file bug reports for earlier versions so we filed it.

[Bug c/68480] -fno-strict-aliasing not respected

2015-11-22 Thread manjeetdahiya at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68480

--- Comment #3 from Manjeet Dahiya  ---
(In reply to Manjeet Dahiya from comment #2)
> (In reply to Andreas Schwab from comment #1)
> > Did you mean gcc-5.1.0?
> 
> No. It is for gcc-4.1.0 :) We were checking for equivalence of code
> generated across different versions and found this bug. We saw that it is
> possible to file bug reports for earlier versions so we filed it.

The reason we tested gcc-4.1.0 was that it is still being used for development
of some of our tools.