Issue 242744
Summary [ARM][Thumb1] A debug read of the writeback base forces an extra SUB in UpdateBaseRegUses
Labels backend:ARM, debuginfo
Assignees
Reporter yqtian-se
    With `-g`, the Thumb1 load/store optimizer emits an extra `subs` after a
writeback `ldm`, so the code differs from `-g0`.

Reproducer (`input.c`):

```c
int f(int *p, int x) {
  int a = p[0];
  int b = p[1];
  int *q = p;
  int c = q[5];
  return a + b * x + c;
}
```

```sh
clang --target=thumbv6m-none-eabi -O2 -ffreestanding -g -c input.c -o g.o
clang --target=thumbv6m-none-eabi -O2 -ffreestanding -g0 -c input.c -o g0.o
llvm-objdump -d --no-show-raw-insn -j .text g.o  > g.txt
llvm-objdump -d --no-show-raw-insn -j .text g0.o > g0.txt
diff g0.txt g.txt
```

Actual (`-g0` vs `-g`, LLVM main `9ce8918945cf`):

```diff
 	ldm	r0!, {r2, r3}
 	muls	r3, r1, r3
 	adds	r1, r3, r2
-	ldr	r0, [r0, #12]
+	subs	r0, #8
+	ldr	r0, [r0, #20]
```

Expected: the offset is folded into the later load under `-g` too.

**Possible cause.** After merging into a writeback `LDM`, `UpdateBaseRegUses`
(https://github.com/llvm/llvm-project/blob/9ce8918945cf/llvm/lib/Target/ARM/ARMLoadStoreOptimizer.cpp#L512) walks the following
instructions. A `DBG_VALUE` that reads the base register (here `q`, which is
`p`) is neither a load/store nor an add/sub it can update, so it inserts a
`tSUBi8` to restore the base before it.

**Why skipping the debug instruction is not enough.** If the walk skips the
`DBG_VALUE`, it then reads the incremented base, so `q` would be shown as
`p + 8`. The debug value needs adjusting: subtract the writeback offset in its
`DIExpression` (as `RISCVMergeBaseOffset` does for folded offsets), or set it
undef.

Related: #237505 and #237017 are other Thumb1 debug-instruction issues.

This issue is assisted by Claude and reviewed by yqtian-se.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to