<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55759>55759</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Also outline conditions that pick which cold BB to go to
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
scottmcm
</td>
</tr>
</table>
<pre>
This rust example https://rust.godbolt.org/z/nddKM8G61
```rust
pub fn demo(x: &[u32]) -> u32 {
x[0] + x[1] + x[2]
}
```
And this C++ example https://cpp.godbolt.org/z/8sM48xddx
```rust
unsigned demo(std::vector<unsigned>& v) {
return v.at(0) + v.at(1) + v.at(2);
}
```
Produce very similar assembly
```nasm
test rsi, rsi
je .LBB0_4 ; <-- Note branch
cmp rsi, 1
je .LBB0_5 ; <-- Note branch
cmp rsi, 2
jbe .LBB0_3 ; <-- Note branch
mov eax, dword ptr [rdi + 4]
add eax, dword ptr [rdi]
add eax, dword ptr [rdi + 8]
pop rcx
ret
.LBB0_4:
lea rdx, [rip + .L__unnamed_1]
xor edi, edi
xor esi, esi
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
.LBB0_5:
lea rdx, [rip + .L__unnamed_2]
mov edi, 1
mov esi, 1
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
.LBB0_3:
lea rdx, [rip + .L__unnamed_3]
mov edi, 2
mov esi, 2
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
```
That `panic_bounds_check` is cold, as is the `__throw_out_of_range_fmt` in the C++ example.
Because those three jumps all lead to cold blocks and the first two are actually subsets of the last one, it would be nice if the "which cold call specifically" jumps could be moved out of the main part of the method so they don't need to get decoded or use icache space.
For example, it could be structured something like this:
```nasm
cmp rsi, 2
jbe .LBB0_3 ; <-- Just the superset branch in the main part
mov eax, dword ptr [rdi + 4]
add eax, dword ptr [rdi]
add eax, dword ptr [rdi + 8]
pop rcx
ret
.LBB0_4:
lea rdx, [rip + .L__unnamed_1]
xor edi, edi
xor esi, esi
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
.LBB0_5:
lea rdx, [rip + .L__unnamed_2]
mov edi, 1
mov esi, 1
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
.LBB0_3:
test rsi, rsi
je .LBB0_4 ; <-- Then the specific checks moved later
cmp rsi, 1
je .LBB0_5 ; <-- Then the specific checks moved later
lea rdx, [rip + .L__unnamed_3]
mov edi, 2
mov esi, 2
call qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
ud2
```
(I don't know enough about the encodings to know if specifically that is the right way -- maybe rearranging the blocks to have forward branches would be better or something -- but hopefully that gets the idea across.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV9tu4zYQ_Rr5hYghS5YvD36Ik91F22y7KPIuUOTYYkKRKi--9Os7pGTHdtx0NyiwQLuGbZrk3M5wPEesNN8vHmthifHWEdjRppVAaudam-S3SfYR32FruNa80tINtVnj0p_4UZz_8nn2aTJK0vskvU0mafcO4t1S6yuyUoRDo5NstkODJMkmSbH0eZYU90k2JzdJ_oHglCTTZadE8LVDmRQlUHwZJ6PTSdTtfE7vL5z30_h9qzhxAdsdagbl6_BY215BN7Ofx7Md57u_ReeVFWsF_IDPOh5s5rcbYE6bJL87SCBGxE02AfAZTgPOG0U2Q-rQQBr3Mc5-PrqYZzhP8uXXQP9iNPcMyAbMnljRCEkNodZCU8n9hZ6itnkJKbwcYC2E8KxIsrs4nO0_QTcOH5bLtBwTDAo_dzc35FftgFSGKlafq7CmJScmR28ZLN5hMLswWMGJwfxrDDZ6E0egu2CQb7XhpHWGYMUZLuJBjI-Vd9CinL-l9a3y0cvslVare7Bsd76BBdQt9EcRCvBMQALtBHn0F7yINnoZPpSlV4o2wMvRK487bbo4eUxvGK7vd-mHyxJhVMow_nEGsHfNtIHur9JSJdizUOuTaVlpr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