Skip to content

Commit 4e72cca

Browse files
committed
RISC-V: bitmanip: improve constant-loading for (1ULL << 31) in DImode
The SINGLE_BIT_MASK_OPERAND() is overly restrictive, triggering for bits above 31 only (to side-step any issues with the negative SImode value 0x80000000/(-1ull << 31)/(1 << 31)). This moves the special handling of this SImode value (i.e. the check for (-1ull << 31) to riscv.cc and relaxes the SINGLE_BIT_MASK_OPERAND() test. With this, the code-generation for loading (1ULL << 31) from: li a0,1 slli a0,a0,31 to: bseti a0,zero,31 gcc/ChangeLog: * config/riscv/riscv.cc (riscv_build_integer_1): Rewrite value as (-1 << 31) for the single-bit case, when operating on (1 << 31) in SImode. * config/riscv/riscv.h (SINGLE_BIT_MASK_OPERAND): Allow for any single-bit value, moving the special case for (1 << 31) to riscv_build_integer_1 (in riscv.c). Signed-off-by: Philipp Tomsich <[email protected]>
1 parent d19b434 commit 4e72cca

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

gcc/config/riscv/riscv.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
420420
/* Simply BSETI. */
421421
codes[0].code = UNKNOWN;
422422
codes[0].value = value;
423+
424+
/* RISC-V sign-extends all 32bit values that live in a 32bit
425+
register. To avoid paradoxes, we thus need to use the
426+
sign-extended (negative) representation (-1 << 31) for the
427+
value, if we want to build (1 << 31) in SImode. This will
428+
then expand to an LUI instruction. */
429+
if (mode == SImode && value == (HOST_WIDE_INT_1U << 31))
430+
codes[0].value = (HOST_WIDE_INT_M1U << 31);
431+
423432
return 1;
424433
}
425434

gcc/config/riscv/riscv.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,10 @@ enum reg_class
528528
(((VALUE) | ((1UL<<31) - IMM_REACH)) == ((1UL<<31) - IMM_REACH) \
529529
|| ((VALUE) | ((1UL<<31) - IMM_REACH)) + IMM_REACH == 0)
530530

531-
/* If this is a single bit mask, then we can load it with bseti. But this
532-
is not useful for any of the low 31 bits because we can use addi or lui
533-
to load them. It is wrong for loading SImode 0x80000000 on rv64 because it
534-
needs to be sign-extended. So we restrict this to the upper 32-bits
535-
only. */
536-
#define SINGLE_BIT_MASK_OPERAND(VALUE) \
537-
(pow2p_hwi (VALUE) && (ctz_hwi (VALUE) >= 32))
531+
/* If this is a single bit mask, then we can load it with bseti. Special
532+
handling of SImode 0x80000000 on RV64 is done in riscv_build_integer_1. */
533+
#define SINGLE_BIT_MASK_OPERAND(VALUE) \
534+
(pow2p_hwi (VALUE))
538535

539536
/* Stack layout; function entry, exit and calling. */
540537

0 commit comments

Comments
 (0)