Skip to content

Commit 7a4a52c

Browse files
linsinan1995kito-cheng
authored andcommitted
PR target/99314: Fix integer signedness issue for cpymem pattern expansion.
Third operand of cpymem pattern is unsigned HOST_WIDE_INT, however we are interpret that as signed HOST_WIDE_INT, that not a problem in most case, but when the value is large than signed HOST_WIDE_INT, it might screw up since we have using that value to calculate the buffer size. 2021-03-05 Sinan Lin <sinan@isrc.iscas.ac.cn> Kito Cheng <kito.cheng@sifive.com> gcc/ChangeLog: * config/riscv/riscv.c (riscv_block_move_straight): Change type to unsigned HOST_WIDE_INT for parameter and local variable with HOST_WIDE_INT type. (riscv_adjust_block_mem): Ditto. (riscv_block_move_loop): Ditto. (riscv_expand_block_move): Ditto. (cherry picked from commit d9f0ade)
1 parent 03cb20e commit 7a4a52c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

gcc/config/riscv/riscv.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -3121,9 +3121,9 @@ riscv_legitimize_call_address (rtx addr)
31213121
Assume that the areas do not overlap. */
31223122

31233123
static void
3124-
riscv_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
3124+
riscv_block_move_straight (rtx dest, rtx src, unsigned HOST_WIDE_INT length)
31253125
{
3126-
HOST_WIDE_INT offset, delta;
3126+
unsigned HOST_WIDE_INT offset, delta;
31273127
unsigned HOST_WIDE_INT bits;
31283128
int i;
31293129
enum machine_mode mode;
@@ -3169,8 +3169,8 @@ riscv_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
31693169
register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
31703170

31713171
static void
3172-
riscv_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
3173-
rtx *loop_reg, rtx *loop_mem)
3172+
riscv_adjust_block_mem (rtx mem, unsigned HOST_WIDE_INT length,
3173+
rtx *loop_reg, rtx *loop_mem)
31743174
{
31753175
*loop_reg = copy_addr_to_reg (XEXP (mem, 0));
31763176

@@ -3185,11 +3185,11 @@ riscv_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
31853185
the memory regions do not overlap. */
31863186

31873187
static void
3188-
riscv_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
3189-
HOST_WIDE_INT bytes_per_iter)
3188+
riscv_block_move_loop (rtx dest, rtx src, unsigned HOST_WIDE_INT length,
3189+
unsigned HOST_WIDE_INT bytes_per_iter)
31903190
{
31913191
rtx label, src_reg, dest_reg, final_src, test;
3192-
HOST_WIDE_INT leftover;
3192+
unsigned HOST_WIDE_INT leftover;
31933193

31943194
leftover = length % bytes_per_iter;
31953195
length -= leftover;
@@ -3234,18 +3234,19 @@ riscv_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
32343234
bool
32353235
riscv_expand_block_move (rtx dest, rtx src, rtx length)
32363236
{
3237+
unsigned HOST_WIDE_INT hwi_length = UINTVAL (length);
32373238
if (CONST_INT_P (length))
32383239
{
3239-
HOST_WIDE_INT factor, align;
3240+
unsigned HOST_WIDE_INT factor, align;
32403241

32413242
align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
32423243
factor = BITS_PER_WORD / align;
32433244

32443245
if (optimize_function_for_size_p (cfun)
3245-
&& INTVAL (length) * factor * UNITS_PER_WORD > MOVE_RATIO (false))
3246+
&& hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false))
32463247
return false;
32473248

3248-
if (INTVAL (length) <= RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)
3249+
if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor))
32493250
{
32503251
riscv_block_move_straight (dest, src, INTVAL (length));
32513252
return true;
@@ -3255,7 +3256,8 @@ riscv_expand_block_move (rtx dest, rtx src, rtx length)
32553256
unsigned min_iter_words
32563257
= RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD;
32573258
unsigned iter_words = min_iter_words;
3258-
HOST_WIDE_INT bytes = INTVAL (length), words = bytes / UNITS_PER_WORD;
3259+
unsigned HOST_WIDE_INT bytes = hwi_length;
3260+
unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD;
32593261

32603262
/* Lengthen the loop body if it shortens the tail. */
32613263
for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++)

0 commit comments

Comments
 (0)