<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/153781>153781</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Unprofitable use of th.ext insn for clamping
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
christian-herber-nxp
</td>
</tr>
</table>
<pre>
This sequence, as seen in https://github.com/embench/embench-iot/blob/master/src/picojpeg/libpicojpeg.c,
```
#include <stdint.h>
uint8_t clamp (int16_t s)
{
if ((uint16_t) s > 255U)
{
if (s < 0)
return 0;
else if (s > 255)
return 255;
}
return (uint8_t) s;
}
```
Leads to suboptimal assembly when compiling for the xtheadbb: https://godbolt.org/z/e7o1dseKW
-march=rv32gc_xtheadbb:
```
clamp(short):
li a1, 255
bgeu a1, a0, .LBB0_2
srli a0, a0, 31
not a0, a0
th.ext a0, a0, 0, 0
.LBB0_2:
zext.b a0, a0
ret
```
-march=rv32gc:
```
clamp(short):
li a1, 255
bgeu a1, a0, .LBB0_2
srli a0, a0, 31
addi a0, a0, -1
.LBB0_2:
zext.b a0, a0
ret
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMlEGzmzgMxz-NuWjCGBECOXDIe69ctsft7PGNbRRwx9isbV7TfvodBzJJ086ey3icOPpJ-UsyEiHowRK1rHph1Vsmljg636rR6xC1sLuRvCS_s5c5k67_3v496gCB_l3IKmL4CiIdyYK2MMY4B1aeGHYMu0HHcZG5chPDjiZJVo33bzvtIsNOGicZdpMIkTzDLnjFsJu1cl9nGhh2RsvbKVcMXxk_pXXg2-InhqW2yiw9AStfQ-y1jfnIyk8rumgbm_cIyohpBoaNtrE4vEcIDI8JqV8YPwHoczIybJYNYHiEAKz8BFhVX1YYbnR6Vo-EvALfgvGjp7h4C5yVd5JMoAf8GvHZIf20urD6bZUOsNk2Vc0mauU27KESn0n0AaKDsEg3Rz0JAyIEmqT5Dt9GsqDcNGuj7QBn5yGOBJc4kuilZOXpuX-ul87E3PnUhx-pdbUr-kB__bPK203Cq5GVb_6jxEG9P4R6EnatfUp-dD6lsBJg9FoeUaSLlApwKxiAHGi52wRPe_755YW_4wMV_Bpjta97WTwA1kX4Cbib4pjTJf7su238dPurVegPusRc_jaKp_iU7FNZ_qRqiL7_BdgVv6S7Pb_L-indrG_L_lgeRUZtUVcVVnte8Wxszw0dKi5rVMiPivd4VrWqzkhHLGtZqEy3yLHiTVEVvNwXPK8bua-VOjSqOjQSBdtzmoQ2uTEfU7qEmQ5hobaoyropMiMkmXAdW4iWvsHVyhDTFPNtctrJZQhsz40OMdzDRB0NtV_s7N1ZRyENwRII3Pl2I7QN9vp-XBul7ZAt3rT_M9xS6O1jN3v3lVQabVdBgWG3Kf5o8b8AAAD__5SOhQk">