On Sun, Dec 14, 2025 at 5:09 AM Takayuki 'January June' Suwa <[email protected]> wrote: > > In the expansion of cstoresi4 insn patterns, LT[U] comparisons where the > second operand is an integer constant are canonicalized to LE[U] ones with > one less than the original. > > /* example */ > int test0(int a) { > return a < 100; > } > unsigned int test1(unsigned int a) { > return a <= 100u; > } > void test2(int a[], int b) { > int i; > for (i = 0; i < 16; ++i) > a[i] = (a[i] <= b); > } > > ;; before (TARGET_SALT) > test0: > entry sp, 32 > movi a8, 0x63 > salt a2, a8, a2 > addi.n a2, a2, -1 ;; unwanted inverting > neg a2, a2 ;; > retw.n > test1: > entry sp, 32 > movi a8, 0x64 > saltu a2, a8, a2 > addi.n a2, a2, -1 ;; unwanted inverting > neg a2, a2 ;; > retw.n > test2: > entry sp, 32 > movi.n a9, 0x10 > loop a9, .L5_LEND > .L5: > l32i.n a8, a2, 0 > salt a8, a3, a8 > addi.n a8, a8, -1 ;; immediate cannot be hoisted out > neg a8, a8 > s32i.n a8, a2, 0 > addi.n a2, a2, 4 > .L5_LEND: > retw.n > > This patch reverts such canonicalization by adding 1 to the comparison value > and then converting it back from LE[U] to LT[U], which better matches the > output machine instructions. This patch also makes it easier to benefit > from other optimizations such as CSE, constant propagation, or loop-invariant > hoisting by XORing the result with a register that has a value of 1, rather > than subtracting 1 and then negating the sign to invert the truth of the > result. > > ;; after (TARGET_SALT) > test0: > entry sp, 32 > movi a8, 0x64 > salt a2, a2, a8 > retw.n > test1: > entry sp, 32 > movi a8, 0x65 > saltu a2, a2, a8 > retw.n > test2: > entry sp, 32 > movi.n a10, 1 ;; hoisted out > movi.n a9, 0x10 > loop a9, .L5_LEND > .L5: > l32i.n a8, a2, 0 > salt a8, a3, a8 > xor a8, a8, a10 > s32i.n a8, a2, 0 > addi.n a2, a2, 4 > .L5_LEND: > retw.n > > gcc/ChangeLog: > > * config/xtensa/xtensa.cc (xtensa_expand_scc_SALT): > New sub-function that emits the SALT/SALTU instructions. > (xtensa_expand_scc): Change the part related to the SALT/SALTU > instructions to a call to the above sub-function. > --- > gcc/config/xtensa/xtensa.cc | 115 ++++++++++++++++++------------------ > 1 file changed, 58 insertions(+), 57 deletions(-)
Regtested for target=xtensa-linux-uclibc, no new regressions. Committed the whole series to master. -- Thanks. -- Max
