[LLVM][MIPS] `-mcompact-branches` is not honored for `J` and `JAL`
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
With `-mcompact-branches=always`, for the following code: `JAL` is not converted into `BALC` nor is `J` converted into `BC`.
Secondarily, a call to a `[[noreturn]]` function is also emitting `JAL` when one would expect it to emit a non-linking branch (such as `BC`) due to the link register being unused, though this is of lesser importance.
```c++
__attribute__((noinline))
int foo0()
{
volatile int a;
return a;
}
int bar0()
{
return foo0();
}
```
Clang output (`-std=gnu++23 -O3 -march=mips32r6 -mcompact-branches=always -fno-PIC`):
```asm
foo0():
$func_begin0 = $tmp0
addiu $sp, $sp, -8
lw $2, 4($sp)
addiu $sp, $sp, 8
jrc $ra
bar0():
$func_begin1 = $tmp3
addiu $sp, $sp, -24
sw $ra, 20($sp)
jal foo0()
nop
lw $ra, 20($sp)
addiu $sp, $sp, 24
jrc $ra
```
GCC output (`-std=gnu++23 -O3 -march=mips32r6 -mcompact-branches=always -fno-PIC -fno-optimize-sibling-calls`):
_`-fno-optimize-sibling-calls` added because Clang is also unable to optimize this into a single jump wheras GCC trivially does - see #57795_
```asm
foo0():
addiu $sp,$sp,-16
lw $2,8($sp)
addiu $sp,$sp,16
jrc $31
bar0():
addiu $sp,$sp,-32
sw $31,28($sp)
balc foo0()
lw $31,28($sp)
addiu $sp,$sp,32
jrc $31
```
LLVM/Clang emits `JAL`, whereas GCC properly emits `BALC`. Worse, Clang emits `JAL` with a `nop` in the delay slot.
Contributor guide
Research direction
Reproduce the MIPS32r6 example with Clang using -O3 -mcompact-branches=always -fno-PIC and compare its assembly with the expected GCC output. Trace the compiler's handling of J and JAL in compact-branch mode; done means JAL becomes BALC and J becomes BC, with the noreturn case considered secondarily.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100