[llvm] [ARM] Select ssat for saturating clamps with a non-zero offset (PR #203854)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 15 02:19:38 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-arm
Author: KavinSai-synthara
<details>
<summary>Changes</summary>
### Summary
On ARM/Thumb2, a saturating clamp to a signed range that is not centered on zero is *not* matched to `ssat`, even though it can be potentially matched to `ssat`. The backend instead emits a long explicit compare/select clamp.
This pattern is extremely common in quantized ML code: an int8 requantize is `clamp(accumulator + output_zero_point, -128, 127)`, and whenever the output zero_point != 0, the instruction count regress from one ssat to ~10 instructions.
The root cause is that InstCombine canonically **sinks the constant out of the min/max** — `clamp(X + C, -128, 127)` becomes `clamp(X, -128-C, 127-C) + C` — and the backend's SSAT matcher (`PerformMinMaxToSatCombine`) only handles a clamp whose bounds already form a saturation range. The offset of the form (`[-128-C, 127-C]`, width 256 = 2⁸) falls through to a generic clamp.
#### Reproducer
```c
#include <stdint.h>
int8_t requant_offset(int32_t acc) { // output zero-point = 96
int32_t v = (acc >> 8) + 96;
if (v < -128) v = -128;
if (v > 127) v = 127;
return (int8_t)v;
}
int8_t requant_zero(int32_t acc) { // output zero-point = 0 (for contrast)
int32_t v = (acc >> 8);
if (v < -128) v = -128;
if (v > 127) v = 127;
return (int8_t)v;
}
```
```
clang -O2 --target=thumbv7em-none-eabihf -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -S issue.c
```
**Actual**
`requant_offset` (zero-point 96):
```asm
asrs r1, r0, #<!-- -->8
cmn.w r1, #<!-- -->224
mvn r1, #<!-- -->223
it gt
asrgt r1, r0, #<!-- -->8
cmp r1, #<!-- -->31
it ge
movge r1, #<!-- -->31
add.w r0, r1, #<!-- -->96
sxtb r0, r0
```
`requant_zero` (zero-point 0):
```asm
ssat r0, #<!-- -->8, r0, asr #<!-- -->8
```
#### Expected
`requant_offset` should select `ssat`, e.g.:
```asm
movs r1, #<!-- -->96
add.w r0, r1, r0, asr #<!-- -->8
ssat r0, #<!-- -->8, r0
subs r0, #<!-- -->96
```
#### Why this is sound
A clamp whose width (hi − lo + 1) is a power of two is just an `ssat` shifted off zero by constant C.
```
clamp(X, lo, hi) == ssat_k(X − C) + C, C = lo + 2^(k-1)
```
e.g. `clamp(X, −224, 31)` == `clamp(X+96, (-225+96), (31+96)) - 96` == `ssat8(X + 96) − 96`
Assisted-by: Claude
---
Full diff: https://github.com/llvm/llvm-project/pull/203854.diff
2 Files Affected:
- (modified) llvm/lib/Target/ARM/ARMISelLowering.cpp (+40-9)
- (added) llvm/test/CodeGen/ARM/ssat-offset.ll (+86)
``````````diff
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index e08a4f5ebda08..63a60736254c1 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -18099,19 +18099,50 @@ static SDValue PerformMinMaxToSatCombine(SDValue Op, SelectionDAG &DAG,
APInt MinC = Min.getConstantOperandAPInt(1);
APInt MaxC = Max.getConstantOperandAPInt(1);
- if (Min.getOpcode() != ISD::SMIN || Max.getOpcode() != ISD::SMAX ||
- !(MinC + 1).isPowerOf2())
+ if (Min.getOpcode() != ISD::SMIN || Max.getOpcode() != ISD::SMAX)
return SDValue();
SDLoc DL(Op);
- if (MinC == ~MaxC)
- return DAG.getNode(ARMISD::SSAT, DL, VT, Input,
- DAG.getConstant(MinC.countr_one(), DL, VT));
- if (MaxC == 0)
- return DAG.getNode(ARMISD::USAT, DL, VT, Input,
- DAG.getConstant(MinC.countr_one(), DL, VT));
- return SDValue();
+ // A clamp whose bounds are already a saturation range maps to a single
+ // SSAT / USAT.
+ if ((MinC + 1).isPowerOf2()) {
+ if (MinC == ~MaxC)
+ return DAG.getNode(ARMISD::SSAT, DL, VT, Input,
+ DAG.getConstant(MinC.countr_one(), DL, VT));
+ if (MaxC == 0)
+ return DAG.getNode(ARMISD::USAT, DL, VT, Input,
+ DAG.getConstant(MinC.countr_one(), DL, VT));
+ }
+
+ // For power-of-two clamp widths, convert the range to be zero-centered,
+ // apply SSAT, and convert the result back.
+ //
+ // Width = Hi - Lo + 1
+ // Center = Lo + Width / 2
+ // Result = ssat(X - Center) + Center
+ //
+ // The idea is to shift the input so that the clamp range is centered
+ // around zero, apply ssat, and then shift the result back.
+ //
+ // For example clamp(X, -118, 137) -> Width = 256,Center = 10, so it becomes
+ // ssat(X - 10, 8) + 10
+
+ APInt Width = MinC - MaxC + 1;
+ if (!Width.isPowerOf2() || Width.isOne())
+ return SDValue();
+ unsigned SatBit = Width.logBase2() - 1; // ssat to SatBit + 1 signed bits
+ APInt Center = MaxC + Width.lshr(1);
+
+ // The rewrite is only valid when X - Center does not overflow;
+ SDValue NegC = DAG.getConstant(-Center, DL, VT);
+ if (DAG.computeOverflowForSignedAdd(Input, NegC) != SelectionDAG::OFK_Never)
+ return SDValue();
+
+ SDValue Shifted = DAG.getNode(ISD::ADD, DL, VT, Input, NegC);
+ SDValue Sat = DAG.getNode(ARMISD::SSAT, DL, VT, Shifted,
+ DAG.getConstant(SatBit, DL, VT));
+ return DAG.getNode(ISD::ADD, DL, VT, Sat, DAG.getConstant(Center, DL, VT));
}
/// PerformMinMaxCombine - Target-specific DAG combining for creating truncating
diff --git a/llvm/test/CodeGen/ARM/ssat-offset.ll b/llvm/test/CodeGen/ARM/ssat-offset.ll
new file mode 100644
index 0000000000000..a22555abb20d8
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/ssat-offset.ll
@@ -0,0 +1,86 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=thumbv7em-none-eabi %s -o - | FileCheck %s
+
+; Tests for recovering SSAT from an "offset" clamp: a clamp(X, Lo, Hi) whose
+; width Width = Hi - Lo + 1 is a power of two but whose bounds are not centered
+; on zero. PerformMinMaxToSatCombine rewrites clamp(X, Lo, Hi) into
+; ssat(X - Center) + Center, with Center = Lo + Width/2, guarded so that
+; X - Center cannot signed-overflow.
+
+declare i32 @llvm.smin.i32(i32, i32)
+declare i32 @llvm.smax.i32(i32, i32)
+
+; A bare clamp to [-224, 31] (width 256). X is range-limited by the ashr, so
+; X + 96 cannot overflow: clamp[-224,31] -> ssat8(X + 96) - 96.
+define i32 @offset_clamp_bare(i32 %x) {
+; CHECK-LABEL: offset_clamp_bare:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: movs r1, #96
+; CHECK-NEXT: add.w r0, r1, r0, asr #8
+; CHECK-NEXT: ssat r0, #8, r0
+; CHECK-NEXT: subs r0, #96
+; CHECK-NEXT: bx lr
+ %s = ashr i32 %x, 8
+ %lo = call i32 @llvm.smax.i32(i32 %s, i32 -224)
+ %hi = call i32 @llvm.smin.i32(i32 %lo, i32 31)
+ ret i32 %hi
+}
+
+; An already-symmetric clamp [-128, 127] is still a bare ssat.
+define i32 @symmetric_clamp(i32 %x) {
+; CHECK-LABEL: symmetric_clamp:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: ssat r0, #8, r0
+; CHECK-NEXT: bx lr
+ %lo = call i32 @llvm.smax.i32(i32 %x, i32 -128)
+ %hi = call i32 @llvm.smin.i32(i32 %lo, i32 127)
+ ret i32 %hi
+}
+
+; An unsigned clamp [0, 255] is still a usat.
+define i32 @unsigned_clamp(i32 %x) {
+; CHECK-LABEL: unsigned_clamp:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: usat r0, #8, r0
+; CHECK-NEXT: bx lr
+ %lo = call i32 @llvm.smax.i32(i32 %x, i32 0)
+ %hi = call i32 @llvm.smin.i32(i32 %lo, i32 255)
+ ret i32 %hi
+}
+
+; X is unconstrained, so X + 96 may signed-overflow and the rewrite is unsound.
+; Must stay an explicit clamp (no ssat).
+define i32 @offset_clamp_may_overflow(i32 %x) {
+; CHECK-LABEL: offset_clamp_may_overflow:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: cmn.w r0, #224
+; CHECK-NEXT: it le
+; CHECK-NEXT: mvnle r0, #223
+; CHECK-NEXT: cmp r0, #31
+; CHECK-NEXT: it ge
+; CHECK-NEXT: movge r0, #31
+; CHECK-NEXT: bx lr
+ %lo = call i32 @llvm.smax.i32(i32 %x, i32 -224)
+ %hi = call i32 @llvm.smin.i32(i32 %lo, i32 31)
+ ret i32 %hi
+}
+
+; Width 151 ([-100, 50]) is not a power of two. No fold.
+define i32 @offset_clamp_non_pow2_width(i32 %x) {
+; CHECK-LABEL: offset_clamp_non_pow2_width:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: mvn r1, #99
+; CHECK-NEXT: asrs r2, r0, #8
+; CHECK-NEXT: cmn.w r2, #100
+; CHECK-NEXT: it gt
+; CHECK-NEXT: asrgt r1, r0, #8
+; CHECK-NEXT: cmp r1, #50
+; CHECK-NEXT: it ge
+; CHECK-NEXT: movge r1, #50
+; CHECK-NEXT: mov r0, r1
+; CHECK-NEXT: bx lr
+ %s = ashr i32 %x, 8
+ %lo = call i32 @llvm.smax.i32(i32 %s, i32 -100)
+ %hi = call i32 @llvm.smin.i32(i32 %lo, i32 50)
+ ret i32 %hi
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/203854
More information about the llvm-commits
mailing list