On Fri, Aug 28, 2026 at 11:37 PM Takayuki 'January June' Suwa
<[email protected]> wrote:
>
> In recent versions of the Xtensa ISA, if any floating-point instructions for
> division, reciprocal, or square root are configured, the "CONST.S" instruc-
> tion, which loads a specific constant value into a floating-point register,
> is also enabled.
>
> This patch enables the use of the previously unused "CONST.S" instruction
> when assigning single-precision floating-point immediate values of either
> +0.f, 1.f, 2.f or 0.5f, and also allows the Reload/LRA pass to handle assign-
> ments of +0.f to address (general-purpose) registers using the "MOVI"
> instruction.
>
>      /* example */
>      float test0(void) {
>        return 0;
>      }
>      void test1(float a[]) {
>        a[0] = -0.f;
>        a[1] = 1;
>        a[2] = 2;
>        a[3] /= 2;
>      }
>      float test2(float a[]) {
>        float r = 0;
>        int i;
>        a--;
>        for (i = 1; i <= 16; ++i)
>          r += a[i] * i;
>        return r;
>      }
>
>      ;; after (-Os)
>      test0:
>         entry   sp, 32
>         movi.n  a2, 0           // a2 = +0.f;
>                                 // avoided "const.s f0, 0; rsr a2, f0"
>         retw.n
>      test:
>         entry   sp, 32
>         const.s f0, 1           // f0 = 1.f;
>         ssi     f0, a2, 4
>         const.s f0, 2           // f0 = 2.f;
>         ssi     f0, a2, 8
>         lsi     f0, a2, 12
>         const.s f1, 3           // f1 = 0.5f;
>         mul.s   f0, f0, f1
>         movi.n  a8, -1          // a8 = -0.f;
>         slli    a8, a8, 31      // (-1 << 31) = -0.f by constantsynth
>         s32i.n  a8, a2, 0
>         ssi     f0, a2, 12
>         retw.n
>      test2:
>         entry   sp, 32
>         movi.n  a9, 1
>         const.s f0, 0           // f0 = +0.f;
>                                 // avoided "movi.n a8, 0; wsr f0, a8"
>         movi.n  a8, 0x10
>         loop    a8, .L4_LEND
>      .L4:
>         float.s f1, a9, 0
>         lsi     f2, a2, 0
>         addi.n  a9, a9, 1
>         madd.s  f0, f2, f1
>         addi.n  a2, a2, 4
>         .L4_LEND:
>         rfr     a2, f0
>         retw.n
>
> gcc/ChangeLog:
>
>         * config/xtensa/constraints.md (Gz, Gc): New FP constant constraint.
>         * gcc/config/xtensa/predicates.md (move_operand):
>         Remove to replace the role with the following two new predicates, in
>         order to make it easier for both humans and compilers to understand.
>         (move_int_operand): New predicate for the second operand of
>         "mov[dshq]i_internal" MD patterns.
>         (move_fp_operand): New predicate for the second operand of
>         "mov[ds]f_internal" MD patterns.
>         (const_float_1_operand): Remove as it is not being used.
>         * config/xtensa/xtensa-protos.h (xtensa_fp_const):
>         New function prototype.
>         * gcc/config/xtensa/xtensa.cc (xtensa_fp_const):
>         New function that returns the corresponding non-negative index number
>         for FP constant values supported by the CONST.S instruction.
>         (print_operand): Add a new format specifier "G" that outputs the index
>         for the FP constant value handled by the "CONST.S" instruction.
>         (xtensa_legitimate_constant_p): Add the non-negative zero set by the
>         "MOVI" instruction and the floating-point constant supported by the
>         "CONST.S" instruction to the evaluation range.
>         * gcc/config/xtensa/xtensa.h (TARGET_HARD_FLOAT_CONST_S):
>         A new macro indicating the conditions under which the "CONST.S"
>         instruction is available.
>         * gcc/config/xtensa/xtensa.md
>         (movdi_internal, movsi_internal, movhi_internal, movqi_internal):
>         Replace the "move_operand" predicate with "move_int_operand".
>         (movsf): Modify the preparation statement used during RTL generation
>         to prevent constant immediates that satisfy "Gz" or "Gc" constraints
>         from being placed in literal pool.
>         (movsf_internal): Replace the "move_operand" predicate with
>         "move_fp_operand", and Add the "MOVI" instruction using the "Gz"
>         constraint and the "CONST.S" instruction using the "Gs" constraint to
>         the alternatives.
>         (movdf_internal): Replace the "move_operand" predicate with
>         "move_fp_operand".
>
> (Note to the target maintainer: This patch assumes that the "type" insn attri-
> bute for "CONST.S" is "farith".  If this is incorrect, please change it to the
> correct value.)
>
> ---
>   gcc/config/xtensa/constraints.md  | 13 ++++++++++++
>   gcc/config/xtensa/predicates.md   | 33 +++++++++++++----------------
>   gcc/config/xtensa/xtensa-protos.h |  1 +
>   gcc/config/xtensa/xtensa.cc       | 33 +++++++++++++++++++++++++++++
>   gcc/config/xtensa/xtensa.h        |  3 +++
>   gcc/config/xtensa/xtensa.md       | 35 +++++++++++++++----------------
>   6 files changed, 82 insertions(+), 36 deletions(-)

Regtested for target=xtensa-linux-uclibc, no new regressions.
Committed to master.

-- 
Thanks.
-- Max

Reply via email to