Issue 77158
Summary Convert clamp into fmaxnum/fminnum pairs.
Labels
Assignees
Reporter hiraditya
    test case

```cpp
float clamp(float a) 
{ 
  const float b = 255.0; 
  const float c = 0.0; 
 
  if(b < a) 
    return b; 
  if(a < c) 
    return c; 
 
  return a; 
}
```

clang compiles to
```asm
clamp:                                  // @clamp
 movi    d1, #0000000000000000
        fcmp    s0, #0.0
        mov w8, #1132396544                 // =0x437f0000
        fmov    s2, w8
 fcsel   s1, s1, s0, mi
        fcmp    s0, s2
        fcsel   s0, s2, s1, gt
        ret
```
but we could have something like
```asm
clamp:                                  // @clamp
// BB#0:                                // %entry
        adrp    x8, .LCPI0_0
        ldr     s1, [x8, :lo12:.LCPI0_0]
        fmov    s2, wzr
        fmaxnm  s0, s0, s2
        fminnm  s0, s0, s1
 ret
```

@sebpop  had a patch a while back https://reviews.llvm.org/D24033 that would do this. Since phabricator is deprecated i'll move the patch here.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to