Skip to content

wrong code error #41109

@llvmbot

Description

@llvmbot
Bugzilla Link 41764
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie

Extended Description

I am not sure whether it is a bug. Maybe it is an undefined behaviour. I cannot understand why the results are different at different optimization level.
Specially, if I change the int64_t to type int, the bug doesn't exist.

> cat report.c
typedef char int8_t;
typedef int int16_t;
typedef int uint16_t;
typedef long int64_t;
typedef char uint8_t;
typedef int uint32_t;

union a {
  int64_t b;
  uint16_t c;
} e;
int8_t d, l, h;
uint16_t *g = &e.c;
uint16_t **i = &g;
int16_t j;
uint16_t ***k = &i;
uint32_t m();
int8_t *n(uint16_t, uint32_t, uint16_t, union a, uint32_t);
uint32_t o() {
  union a p = {};
  m(n(d, 0, 0, p, d));
  return l;
}
uint32_t m() {
  int64_t *ad = &e.b;
  *ad = 0;
  int16_t *ae = &j;
  *ae = ***k;
  return h;
}
int8_t *n(uint16_t f, uint32_t ag, uint16_t ah, union a ai, uint32_t aj) {
  int8_t *ak = &d;
  (***k)++;
  return ak;
}
int main() {
  o();
  printf("%d\n", j);
}
> clang -v 
clang version 9.0.0 (trunk 356835) (llvm/trunk 356836)
......

> clang report.c -O0 -o a0
> clang report.c -O2 -o a2
>./a0
0
>./a2
1

Metadata

Metadata

Assignees

No one assigned

    Labels

    TBAAType-Based Alias Analysis / Strict AliasingbugzillaIssues migrated from bugzillacclang:codegenIR generation bugs: mangling, exceptions, etc.undefined behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions