Skip to content

JIT: simple range check is not branchless #96819

Open
@EgorBo

Description

@EgorBo
bool IsInRange(int value, int lower, int upper) => 
    value >= lower && value <= upper;

Current arm64 codegen:

; Method Program:IsInRange(int,int,int):ubyte:this (FullOpts)
G_M32893_IG01:  ;; offset=0x0000
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp
G_M32893_IG02:  ;; offset=0x0008
            cmp     w1, w2
            blt     G_M32893_IG05
G_M32893_IG03:  ;; offset=0x0010
            cmp     w1, w3
            cset    x0, le
G_M32893_IG04:  ;; offset=0x0018
            ldp     fp, lr, [sp], #0x10
            ret     lr
G_M32893_IG05:  ;; offset=0x0020
            mov     w0, wzr
G_M32893_IG06:  ;; offset=0x0024
            ldp     fp, lr, [sp], #0x10
            ret     lr
; Total bytes of code: 44

Expected codgen:

G_M32893_IG01:  ;; offset=0x0000
            stp     fp, lr, [sp, #-0x10]!
            mov     fp, sp
G_M32893_IG02:  ;; offset=0x0008
            cmp     w1, w2
            ccmp    w1, w3, #0, ge
            cset    w1, le
            ret
G_M32893_IG06:  ;; offset=0x0024
            ldp     fp, lr, [sp], #0x10
            ret     lr
; Total bytes of code: 32

(based on https://godbolt.org/z/oqjfMPosj)

cc @jakobbotsch @dotnet/jit-contrib

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions