Issue 242753
Summary [X86] A debug value blocks the LEA+ALU fold in FixupLEAs
Labels backend:X86, debuginfo
Assignees
Reporter yqtian-se
    With `-g`, `X86FixupLEAs::optLEAALU` does not fold a `lea` into the following
`add`, so the code differs from `-g0`.

Reproducer (`input.c`):

```c
extern int G;
int g3(int, int, int);
int f2(int a, int b, int c) {
  int t = a + b;
  int y = c;
  return g3(G, b, y + t);
}
```

```sh
clang --target=i686-linux-gnu -O2 -mregparm=3 -g  -c input.c -o g.o
clang --target=i686-linux-gnu -O2 -mregparm=3 -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
-	addl	%eax, %ecx
-	addl	%edx, %ecx
+	leal	(%eax,%edx), %esi
 	movl	G@GOT(%ebx), %eax
 	movl	(%eax), %eax
+	addl	%esi, %ecx
```

Expected: the same fold under `-g`.

**Possible cause.** `checkRegUsage`
(https://github.com/llvm/llvm-project/blob/9ce8918945cf/llvm/lib/Target/X86/X86FixupLEAs.cpp#L477) scans the instructions between
the `LEA` and the `ADD` and sets `AluDestRef` for any operand that overlaps the
`ADD`'s destination, including the `DBG_VALUE` of `y` (in `%ecx`). Because the
base register is redefined in between (`BaseIndexDef`), `optLEAALU` then gives
up.

**Why skipping the debug instruction is not enough.** In this case `optLEAALU`
inserts the two new `add`s at the position of the `LEA`, before the
`DBG_VALUE`. If the scan ignored the `DBG_VALUE`, it would then read `%ecx`
after the adds, and `y` would be shown as `y + t`. The debug value would need
to be set undef, or kept describing the old value in some other way.

Related: #222868 and #222871 fixed the `FixupLEAs` search distance, a different check.

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