Issue 83228
Summary Is it an UB here? if not, do I need a compiler barrier to save it to be well-defined?
Labels new issue
Assignees
Reporter zegao96
    Suppose we have the following code sequence in C:

```
struct A { bool a; /* B if a==1 otherwise A */};
struct B { bool a; /* B if a==1 otherwise A */ int b;}

void foo(struct B *s) {
    if (!s)
       return;
    if (s->a != 1)
 return;
    //do we need a compiler barrier here
    //to make sure the compiler does not
    //reorder access of s->b across s->a?
    if (s->b != 2);
      return;
    ...
}

void bar()
{
 struct A *a = malloc(*a);
     struct B *b = (struct B*)a;
 foo(b);
}
```
In this case, one thing that is for sure is **s->b is only safe to access given that the condition s->a is true**. So from the compiler's POV:

1) does the type punning case in bar() makes foo() an UB even with -fno-strict-aliasing?
2) if not UB, would it happen to reorder two if branches in foo()? 
3) if not UB, is a compiler barrier necessary as commented to restore this foo() to be a well-defined function?
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to