godbolt
public static Span<char> Trim(Span<char> s)
{
while (s.Length != 0 && s[ 0] == '/') s = s.Slice(1);
while (s.Length != 0 && s[^1] == '/') s = s.Slice(0, s.Length - 1);
return s;
}
Current codegen:
coreclr trunk-20260521+2a65f102aaad2e0b97bf4554dd510ae57801ae5e
Program:Trim(System.Span`1[char]):System.Span`1[char] (FullOpts):
push rbp
mov rbp, rsp
test esi, esi
je SHORT G_M48643_IG04 ; <-- 1: could jump directly to G_M48643_IG06
align [0 bytes for IG03]
G_M48643_IG03:
cmp word ptr [rdi], 47
jne SHORT G_M48643_IG04 ; <-- 2: could jump directly to G_M48643_IG05
add rdi, 2
dec esi
jne SHORT G_M48643_IG03
G_M48643_IG04:
test esi, esi ; <-- 3: dead test
je SHORT G_M48643_IG06 ; jmp SHORT G_M48643_IG06
align [6 bytes for IG05]
G_M48643_IG05:
lea eax, [rsi-0x01]
mov edx, eax
cmp word ptr [rdi+2*rdx], 47
jne SHORT G_M48643_IG06
mov esi, eax
test esi, esi
jne SHORT G_M48643_IG05
G_M48643_IG06:
mov rax, rdi
mov rdx, rsi
pop rbp
ret
- since
G_M48643_IG04 immediately repeats the same condition
- we already know
esi != 0
- the condition is already known
esi == 0
godbolt
Current codegen:
coreclr trunk-20260521+2a65f102aaad2e0b97bf4554dd510ae57801ae5e
G_M48643_IG04immediately repeats the same conditionesi != 0esi == 0