<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54454>54454</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang misoptimize for-loop whose body should not be executed
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zxcuiop
      </td>
    </tr>
</table>

<pre>
    The code:
```
extern int printf(const char *, ...);
extern void abort(void);

typedef struct {
  long r[(19 + sizeof(long)) / (sizeof(long))];
} realvaluetype;

typedef void *tree;

static realvaluetype real_value_from_int_cst(
    tree x, tree y) {
  realvaluetype r;
  int i, j = 0, k = 0;
  int value = 1;
  int *arr[] = {&value};
  for (i = 0; ((int)0x80000000 / (*arr[j])) <
              sizeof(r.r) / sizeof(long);
       ++k) {
    r.r[i] = -1;
  }
  return r;
}

struct brfic_args {
  tree type;
  tree i;
  realvaluetype d;
};

static void build_real_from_int_cst_1(data) void *data;
{
  struct brfic_args *args = (struct brfic_args *)data;
  args->d = real_value_from_int_cst(args->type, args->i);
}

int main() {
  struct brfic_args args;

  __builtin_memset(&args, 0, sizeof(args));
  build_real_from_int_cst_1(&args);

  printf("%d\n", args.d.r[0] == 0);
  return 0;
}
```

sanitize ub:
```
$ clang-12 -fsanitize=undefined a.c -o a.out && ./a.out
0
$ gcc-10 -fsanitize=undefined a.c -o a.out && ./a.out
0
```

the misoptimization:
```
$ clang-12 -O0 a.c -o a.out &&  ./a.out                
1
$ clang-12 -O1 a.c -o a.out &&  ./a.out                
0
```

A strange case: once the `*arr[j]` is replaced by `*arr[0]` (they should be identical), clang will correctly optimize the for-loop.

my clang version:
```
$ clang-12 -v
Ubuntu clang version 12.0.0-3ubuntu1~20.04.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/lib/cuda, version 10.1
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1V1uTojgU_jX4koIK4aI88NCt49Y-7cPOPFsBoqYnECuXbu1fvycBRRy7t2tqpBRJcq7f-XKClWxO5fc9Q7VsWJA8BXgV4Kcgx8PHD9nRMNUh3hl0UHDfBmRRy04bVO-pQgF5CsgSRVEUkCJInidKr5I3iFZSGVByg2uZ_m5OB9awLdJG2dqgYD6sIiRkt0MqyJ5BNy7A0TPS_J1JF4Bbc7aIm1_Dd3FnKchWo7P5CilGxSsVljmfH4ThI4acjGK3ItpQw-upFT_a-OFmq2S7AYA2tXbpntNAyNlCR4eSfzr5qMc8bwxevCKPOXd6LyhIVgi7x5_D41TK6_uV-GYFcqHKo5itvIDzTHKvAKBcSW-lq-aCXxy4kZvoIJ0CHxe4v86IXyy_OKCHYiTLMe_xuhRHRepcs18KNkbiL6g3fH7egAVwRc4nP6cTXifsErqgaiwwUF0zYFpNz7dKbXm9oWqnr734Ok1YMszxq4lp3ZqJo7vM8dyqLBfNxtPmmjCbGJBoqKEu3zMJ_Xg0e_F8J3ZXC_frCgyb4Z4AWJ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