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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Debugging Error: Non-null pointer `(Instruction *)I` shows as null
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    When debugging with gdb/lldb for `build/bin/clang`, encountered a situation where `(Instrcution *)I` appears to be null, but in reality, it is not.

This is the `clang` I installed (commit 35c19fdde2583e74d940f6cd47b97a5c28bfe368). The results are the same whether using `Release/17.x` or `-DCMAKE_BUILD_TYPE=Debug`. Subsequently, I refer to it as `$HOME/usr/clang`.
```shell
cmake -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="X86" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$HOME/usr" -GNinja ../llvm
ninja install
```

Then I used the aforementioned `$HOME/usr/clang` to compile `build/bin/clang`, using the parameters based on the same commit (35c19fdde2583e74d940f6cd47b97a5c28bfe368). This issue does not occur when using `gcc 11.4.0`.
```shell
cmake -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="X86;RISCV" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=$HOME/usr/bin/clang -DCMAKE_CXX_COMPILER=$HOME/usr/bin/clang++ -GNinja ../llvm
ninja clang
```

Then, run `gdb` to debug `build/bin/clang` for compiling `foo.c` (lldb has the same issue):

`foo.c`:
```c
void foo(int *restrict A, int *restrict B, int *restrict C) {
  for (int i = 0; i < 64; ++i)
    A[i] = B[C[i]];
}
```

```shell
gdb --args build/bin/clang foo.c --target=riscv64 -march=rv64gc -O3 -mllvm -debug-only=instcombine -S
b llvm-project/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:4416
```

After 2 `(gdb) c` ...

```shell
IC: Visiting:   %idxprom1 = sext i32 %0 to i64
IC: Visiting:   %arrayidx2 = getelementptr inbounds i32, ptr %B, i64 %idxprom1
IC: Visiting:   %1 = load i32, ptr %arrayidx2, align 4, !tbaa !4
IC: Visiting:   %arrayidx4 = getelementptr inbounds i32, ptr %A, i64 %idxprom
IC: Visiting:   store i32 %1, ptr %arrayidx4, align 4, !tbaa !4
IC: Visiting:   %inc = add nsw i32 %i.0, 1

Breakpoint 1, llvm::InstCombinerImpl::run (this=0x7fffffff6e10) at llvm-project/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:4416
4416 if (Result != I) {
(gdb) p I
$1 = (llvm::Instruction *) 0x0
(gdb) n
4449            LLVM_DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n'
(gdb) p I
$2 = (llvm::Instruction *) 0x0
(gdb) p Result
$3 = (llvm::Instruction *) 0x5555649ffa00
(gdb) p Result!=I
$4 = true
(gdb) 
```

Here, `I` is not actually a null pointer; otherwise, the code within the `else` branch (line 4449) would not be executed.


Related Codes: https://github.com/llvm/llvm-project/blob/35c19fdde2583e74d940f6cd47b97a5c28bfe368/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp#L4416
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V19z4jgS_zTipQuXLf_BfuABMMxyl0ymCDs395SSrcbWrpE4SZ4k3_5KMgSSS7jsXt1SFMgt9b9f_9ySmDGikYhTks5JWo5Yb1ulp6aXpmWqxlGl-PP0Hy1K4Fj1TSNkA4_CttDwitBV1_EKdkoDycKqFx0ndFUJSeiq7phsSBYSugCUteqlRY0cGBhhe2aFkvDYokbwi_K1NFbXvZcTOiO0WJMsBHY4INMGrIIKQfZd5wxWvQUhQSPrhH12EmFBGJDKBiQsSTgbfretME5uW-_mFBOsQUhjWdchB0LzWu33wkKc1lGx4xxpmsc4SXiRhLus5smkKiYsrWle7TDOckKLALYtgkbTd9YA0-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