[clang] [llvm] [MIPS][ISel] Fix musttail (PR #161860)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 30 11:02:54 PDT 2025


ameisen wrote:

This PR doesn't fix (if it was intended to) sibling call optimizations.

It _does_ change the code generation for them, however, but for the worst.

```c++
__attribute__((noinline))
int foo0()
{
	volatile int a;
	return a += 1;
}

int bar0()
{
	return foo0();
}
```

with tail calls disabled (`-std=gnu++23 -O3 -march=mips32r6 -fno-PIC -mllvm -mips-tail-calls=0`):

```asm
	addiu	$sp, $sp, -24
	sw	$ra, 20($sp)                    # 4-byte Folded Spill
	sw	$fp, 16($sp)                    # 4-byte Folded Spill
	move	$fp, $sp
	jal	_Z4foo0v
	nop
	move	$sp, $fp
	lw	$fp, 16($sp)                    # 4-byte Folded Reload
	lw	$ra, 20($sp)                    # 4-byte Folded Reload
	addiu	$sp, $sp, 24
	jrc	$ra
```

with tail calls enabled (`-std=gnu++23 -O3 -march=mips32r6 -fno-PIC -mllvm -mips-tail-calls=1`):

```asm
	addiu	$sp, $sp, -8
	sw	$ra, 4($sp)                     # 4-byte Folded Spill
	sw	$fp, 0($sp)                     # 4-byte Folded Spill
	move	$fp, $sp
	move	$sp, $fp
	lw	$fp, 0($sp)                     # 4-byte Folded Reload
	lw	$ra, 4($sp)                     # 4-byte Folded Reload
	j	_Z4foo0v
	addiu	$sp, $sp, 8
```

So, it's moving the `j` to the end, but it's not cleaning up the stack changes that were _around_ the jump. Everything in here but the `j` could be removed (with a `nop`, but the `j` should be replaced with a `bc` anyways), but this effectively is generating a rather messed up function.

https://github.com/llvm/llvm-project/pull/161860


More information about the cfe-commits mailing list