| 1 |
/* Subroutines used to expand string operations for RISC-V. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published |
| 8 |
by the Free Software Foundation; either version 3, or (at your |
| 9 |
option) any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 14 |
License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#define IN_TARGET_CODE 1 |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "backend.h" |
| 26 |
#include "rtl.h" |
| 27 |
#include "tree.h" |
| 28 |
#include "memmodel.h" |
| 29 |
#include "tm_p.h" |
| 30 |
#include "ira.h" |
| 31 |
#include "print-tree.h" |
| 32 |
#include "varasm.h" |
| 33 |
#include "explow.h" |
| 34 |
#include "expr.h" |
| 35 |
#include "output.h" |
| 36 |
#include "target.h" |
| 37 |
#include "predict.h" |
| 38 |
#include "optabs.h" |
| 39 |
#include "riscv-protos.h" |
| 40 |
#include "recog.h" |
| 41 |
#include "tm-constrs.h" |
| 42 |
|
| 43 |
/* Emit proper instruction depending on mode of dest. */ |
| 44 |
|
| 45 |
#define GEN_EMIT_HELPER2(name) \ |
| 46 |
static rtx_insn * \ |
| 47 |
do_## name ## 2(rtx dest, rtx src) \ |
| 48 |
{ \ |
| 49 |
rtx_insn *insn; \ |
| 50 |
if (GET_MODE (dest) == DImode) \ |
| 51 |
insn = emit_insn (gen_ ## name ## di2 (dest, src)); \ |
| 52 |
else \ |
| 53 |
insn = emit_insn (gen_ ## name ## si2 (dest, src)); \ |
| 54 |
return insn; \ |
| 55 |
} |
| 56 |
|
| 57 |
/* Emit proper instruction depending on mode of dest. */ |
| 58 |
|
| 59 |
#define GEN_EMIT_HELPER3(name) \ |
| 60 |
static rtx_insn * \ |
| 61 |
do_## name ## 3(rtx dest, rtx src1, rtx src2) \ |
| 62 |
{ \ |
| 63 |
rtx_insn *insn; \ |
| 64 |
if (GET_MODE (dest) == DImode) \ |
| 65 |
insn = emit_insn (gen_ ## name ## di3 (dest, src1, src2)); \ |
| 66 |
else \ |
| 67 |
insn = emit_insn (gen_ ## name ## si3 (dest, src1, src2)); \ |
| 68 |
return insn; \ |
| 69 |
} |
| 70 |
|
| 71 |
GEN_EMIT_HELPER3(add) /* do_add3 */ |
| 72 |
GEN_EMIT_HELPER3(and) /* do_and3 */ |
| 73 |
GEN_EMIT_HELPER3(ashl) /* do_ashl3 */ |
| 74 |
GEN_EMIT_HELPER2(bswap) /* do_bswap2 */ |
| 75 |
GEN_EMIT_HELPER2(clz) /* do_clz2 */ |
| 76 |
GEN_EMIT_HELPER2(ctz) /* do_ctz2 */ |
| 77 |
GEN_EMIT_HELPER3(ior) /* do_ior3 */ |
| 78 |
GEN_EMIT_HELPER3(ior_not) /* do_ior_not3 */ |
| 79 |
GEN_EMIT_HELPER3(lshr) /* do_lshr3 */ |
| 80 |
GEN_EMIT_HELPER2(neg) /* do_neg2 */ |
| 81 |
GEN_EMIT_HELPER2(orcb) /* do_orcb2 */ |
| 82 |
GEN_EMIT_HELPER2(one_cmpl) /* do_one_cmpl2 */ |
| 83 |
GEN_EMIT_HELPER3(rotr) /* do_rotr3 */ |
| 84 |
GEN_EMIT_HELPER3(sub) /* do_sub3 */ |
| 85 |
GEN_EMIT_HELPER2(th_rev) /* do_th_rev2 */ |
| 86 |
GEN_EMIT_HELPER2(th_tstnbz) /* do_th_tstnbz2 */ |
| 87 |
GEN_EMIT_HELPER3(xor) /* do_xor3 */ |
| 88 |
GEN_EMIT_HELPER2(zero_extendqi) /* do_zero_extendqi2 */ |
| 89 |
GEN_EMIT_HELPER2(zero_extendhi) /* do_zero_extendhi2 */ |
| 90 |
|
| 91 |
#undef GEN_EMIT_HELPER2 |
| 92 |
#undef GEN_EMIT_HELPER3 |
| 93 |
|
| 94 |
/* Helper function to emit zero-extended loads. |
| 95 |
|
| 96 |
MODE is the mode to use for the load. |
| 97 |
DEST is the destination register for the data. |
| 98 |
MEM is the source to load from. */ |
| 99 |
|
| 100 |
static void |
| 101 |
do_load (machine_mode mode, rtx dest, rtx mem) |
| 102 |
{ |
| 103 |
if (mode == QImode) |
| 104 |
do_zero_extendqi2 (dest, mem); |
| 105 |
else if (mode == HImode) |
| 106 |
do_zero_extendhi2 (dest, mem); |
| 107 |
else if (mode == SImode && TARGET_64BIT) |
| 108 |
emit_insn (gen_zero_extendsidi2 (dest, mem)); |
| 109 |
else if (mode == Xmode) |
| 110 |
emit_move_insn (dest, mem); |
| 111 |
else |
| 112 |
gcc_unreachable (); |
| 113 |
} |
| 114 |
|
| 115 |
/* Helper function to emit zero-extended loads. |
| 116 |
|
| 117 |
MODE is the mode to use for the load (QImode or Pmode). |
| 118 |
DEST is the destination register for the data. |
| 119 |
ADDR_REG is the register that holds the address. |
| 120 |
ADDR is the address expression to load from. */ |
| 121 |
|
| 122 |
static void |
| 123 |
do_load_from_addr (machine_mode mode, rtx dest, rtx addr_reg, rtx addr) |
| 124 |
{ |
| 125 |
rtx mem = gen_rtx_MEM (mode, addr_reg); |
| 126 |
MEM_COPY_ATTRIBUTES (mem, addr); |
| 127 |
set_mem_size (mem, GET_MODE_SIZE (mode)); |
| 128 |
|
| 129 |
do_load (mode, dest, mem); |
| 130 |
} |
| 131 |
|
| 132 |
/* Generate a sequence to compare single characters in data1 and data2. |
| 133 |
|
| 134 |
RESULT is the register where the return value of str(n)cmp will be stored. |
| 135 |
DATA1 is a register which contains character1. |
| 136 |
DATA2 is a register which contains character2. |
| 137 |
FINAL_LABEL is the location after the calculation of the return value. */ |
| 138 |
|
| 139 |
static void |
| 140 |
emit_strcmp_scalar_compare_byte (rtx result, rtx data1, rtx data2, |
| 141 |
rtx final_label) |
| 142 |
{ |
| 143 |
do_sub3 (result, data1, data2); |
| 144 |
emit_jump_insn (gen_jump (final_label)); |
| 145 |
emit_barrier (); /* No fall-through. */ |
| 146 |
} |
| 147 |
|
| 148 |
/* Generate a sequence to compare two strings in data1 and data2. |
| 149 |
|
| 150 |
DATA1 is a register which contains string1. |
| 151 |
DATA2 is a register which contains string2. |
| 152 |
ORC1 is a register where orc.b(data1) will be stored. |
| 153 |
CMP_BYTES is the length of the strings. |
| 154 |
END_LABEL is the location of the code that calculates the return value. */ |
| 155 |
|
| 156 |
static void |
| 157 |
emit_strcmp_scalar_compare_subword (rtx data1, rtx data2, rtx orc1, |
| 158 |
unsigned HOST_WIDE_INT cmp_bytes, |
| 159 |
rtx end_label) |
| 160 |
{ |
| 161 |
/* Set a NUL-byte after the relevant data (behind the string). */ |
| 162 |
long long im = -256ll; |
| 163 |
rtx imask = gen_rtx_CONST_INT (Xmode, im); |
| 164 |
rtx m_reg = gen_reg_rtx (Xmode); |
| 165 |
emit_insn (gen_rtx_SET (m_reg, imask)); |
| 166 |
do_rotr3 (m_reg, m_reg, GEN_INT (BITS_PER_WORD - cmp_bytes * BITS_PER_UNIT)); |
| 167 |
do_and3 (data1, m_reg, data1); |
| 168 |
do_and3 (data2, m_reg, data2); |
| 169 |
if (TARGET_ZBB) |
| 170 |
do_orcb2 (orc1, data1); |
| 171 |
else |
| 172 |
do_th_tstnbz2 (orc1, data1); |
| 173 |
emit_jump_insn (gen_jump (end_label)); |
| 174 |
emit_barrier (); /* No fall-through. */ |
| 175 |
} |
| 176 |
|
| 177 |
/* Generate a sequence to compare two strings in data1 and data2. |
| 178 |
|
| 179 |
DATA1 is a register which contains string1. |
| 180 |
DATA2 is a register which contains string2. |
| 181 |
ORC1 is a register where orc.b(data1) will be stored. |
| 182 |
TESTVAL is the value to test ORC1 against. |
| 183 |
END_LABEL is the location of the code that calculates the return value. |
| 184 |
NONUL_END_LABEL is the location of the code that calculates the return value |
| 185 |
in case the first string does not contain a NULL-byte. */ |
| 186 |
|
| 187 |
static void |
| 188 |
emit_strcmp_scalar_compare_word (rtx data1, rtx data2, rtx orc1, rtx testval, |
| 189 |
rtx end_label, rtx nonul_end_label) |
| 190 |
{ |
| 191 |
/* Check if data1 contains a NUL character. */ |
| 192 |
if (TARGET_ZBB) |
| 193 |
do_orcb2 (orc1, data1); |
| 194 |
else |
| 195 |
do_th_tstnbz2 (orc1, data1); |
| 196 |
rtx cond1 = gen_rtx_NE (VOIDmode, orc1, testval); |
| 197 |
emit_unlikely_jump_insn (gen_cbranch4 (Pmode, cond1, orc1, testval, |
| 198 |
end_label)); |
| 199 |
/* Break out if u1 != u2 */ |
| 200 |
rtx cond2 = gen_rtx_NE (VOIDmode, data1, data2); |
| 201 |
emit_unlikely_jump_insn (gen_cbranch4 (Pmode, cond2, data1, |
| 202 |
data2, nonul_end_label)); |
| 203 |
/* Fall-through on equality. */ |
| 204 |
} |
| 205 |
|
| 206 |
/* Generate the sequence of compares for strcmp/strncmp using zbb instructions. |
| 207 |
|
| 208 |
RESULT is the register where the return value of str(n)cmp will be stored. |
| 209 |
The strings are referenced by SRC1 and SRC2. |
| 210 |
The number of bytes to compare is defined by NBYTES. |
| 211 |
DATA1 is a register where string1 will be stored. |
| 212 |
DATA2 is a register where string2 will be stored. |
| 213 |
ORC1 is a register where orc.b(data1) will be stored. |
| 214 |
END_LABEL is the location of the code that calculates the return value. |
| 215 |
NONUL_END_LABEL is the location of the code that calculates the return value |
| 216 |
in case the first string does not contain a NULL-byte. |
| 217 |
FINAL_LABEL is the location of the code that comes after the calculation |
| 218 |
of the return value. */ |
| 219 |
|
| 220 |
static void |
| 221 |
emit_strcmp_scalar_load_and_compare (rtx result, rtx src1, rtx src2, |
| 222 |
unsigned HOST_WIDE_INT nbytes, |
| 223 |
rtx data1, rtx data2, rtx orc1, |
| 224 |
rtx end_label, rtx nonul_end_label, |
| 225 |
rtx final_label) |
| 226 |
{ |
| 227 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 228 |
unsigned HOST_WIDE_INT offset = 0; |
| 229 |
|
| 230 |
rtx testval = gen_reg_rtx (Xmode); |
| 231 |
if (TARGET_ZBB) |
| 232 |
emit_insn (gen_rtx_SET (testval, constm1_rtx)); |
| 233 |
else |
| 234 |
emit_insn (gen_rtx_SET (testval, const0_rtx)); |
| 235 |
|
| 236 |
while (nbytes > 0) |
| 237 |
{ |
| 238 |
unsigned HOST_WIDE_INT cmp_bytes = xlen < nbytes ? xlen : nbytes; |
| 239 |
machine_mode load_mode; |
| 240 |
if (cmp_bytes == 1) |
| 241 |
load_mode = QImode; |
| 242 |
else |
| 243 |
load_mode = Xmode; |
| 244 |
|
| 245 |
rtx addr1 = adjust_address (src1, load_mode, offset); |
| 246 |
do_load (load_mode, data1, addr1); |
| 247 |
rtx addr2 = adjust_address (src2, load_mode, offset); |
| 248 |
do_load (load_mode, data2, addr2); |
| 249 |
|
| 250 |
if (cmp_bytes == 1) |
| 251 |
{ |
| 252 |
emit_strcmp_scalar_compare_byte (result, data1, data2, final_label); |
| 253 |
return; |
| 254 |
} |
| 255 |
else if (cmp_bytes < xlen) |
| 256 |
{ |
| 257 |
emit_strcmp_scalar_compare_subword (data1, data2, orc1, |
| 258 |
cmp_bytes, end_label); |
| 259 |
return; |
| 260 |
} |
| 261 |
else |
| 262 |
emit_strcmp_scalar_compare_word (data1, data2, orc1, testval, |
| 263 |
end_label, nonul_end_label); |
| 264 |
|
| 265 |
offset += cmp_bytes; |
| 266 |
nbytes -= cmp_bytes; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/* Fixup pointers and generate a call to strcmp. |
| 271 |
|
| 272 |
RESULT is the register where the return value of str(n)cmp will be stored. |
| 273 |
The strings are referenced by SRC1 and SRC2. |
| 274 |
The number of already compared bytes is defined by NBYTES. */ |
| 275 |
|
| 276 |
static void |
| 277 |
emit_strcmp_scalar_call_to_libc (rtx result, rtx src1, rtx src2, |
| 278 |
unsigned HOST_WIDE_INT nbytes) |
| 279 |
{ |
| 280 |
/* Update pointers past what has been compared already. */ |
| 281 |
rtx src1_addr = force_reg (Pmode, XEXP (src1, 0)); |
| 282 |
rtx src2_addr = force_reg (Pmode, XEXP (src2, 0)); |
| 283 |
rtx src1_new = force_reg (Pmode, |
| 284 |
gen_rtx_PLUS (Pmode, src1_addr, GEN_INT (nbytes))); |
| 285 |
rtx src2_new = force_reg (Pmode, |
| 286 |
gen_rtx_PLUS (Pmode, src2_addr, GEN_INT (nbytes))); |
| 287 |
|
| 288 |
/* Construct call to strcmp to compare the rest of the string. */ |
| 289 |
tree fun = builtin_decl_explicit (BUILT_IN_STRCMP); |
| 290 |
emit_library_call_value (XEXP (DECL_RTL (fun), 0), |
| 291 |
result, LCT_NORMAL, GET_MODE (result), |
| 292 |
src1_new, Pmode, src2_new, Pmode); |
| 293 |
} |
| 294 |
|
| 295 |
/* Fast strcmp-result calculation if no NULL-byte in string1. |
| 296 |
|
| 297 |
RESULT is the register where the return value of str(n)cmp will be stored. |
| 298 |
The mismatching strings are stored in DATA1 and DATA2. */ |
| 299 |
|
| 300 |
static void |
| 301 |
emit_strcmp_scalar_result_calculation_nonul (rtx result, rtx data1, rtx data2) |
| 302 |
{ |
| 303 |
/* Words don't match, and no NUL byte in one word. |
| 304 |
Get bytes in big-endian order and compare as words. */ |
| 305 |
do_bswap2 (data1, data1); |
| 306 |
do_bswap2 (data2, data2); |
| 307 |
/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence. */ |
| 308 |
rtx tmp = gen_reg_rtx (Xmode); |
| 309 |
emit_insn (gen_slt_3 (LTU, Xmode, Xmode, tmp, data1, data2)); |
| 310 |
do_neg2 (tmp, tmp); |
| 311 |
do_ior3 (result, tmp, const1_rtx); |
| 312 |
} |
| 313 |
|
| 314 |
/* strcmp-result calculation. |
| 315 |
|
| 316 |
RESULT is the register where the return value of str(n)cmp will be stored. |
| 317 |
The strings are stored in DATA1 and DATA2. |
| 318 |
ORC1 contains orc.b(DATA1). */ |
| 319 |
|
| 320 |
static void |
| 321 |
emit_strcmp_scalar_result_calculation (rtx result, rtx data1, rtx data2, |
| 322 |
rtx orc1) |
| 323 |
{ |
| 324 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 325 |
|
| 326 |
/* Convert non-equal bytes into non-NUL bytes. */ |
| 327 |
rtx diff = gen_reg_rtx (Xmode); |
| 328 |
do_xor3 (diff, data1, data2); |
| 329 |
rtx shift = gen_reg_rtx (Xmode); |
| 330 |
|
| 331 |
if (TARGET_ZBB) |
| 332 |
{ |
| 333 |
/* Convert non-equal or NUL-bytes into non-NUL bytes. */ |
| 334 |
rtx syndrome = gen_reg_rtx (Xmode); |
| 335 |
do_orcb2 (diff, diff); |
| 336 |
do_ior_not3 (syndrome, orc1, diff); |
| 337 |
/* Count the number of equal bits from the beginning of the word. */ |
| 338 |
do_ctz2 (shift, syndrome); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
/* Convert non-equal or NUL-bytes into non-NUL bytes. */ |
| 343 |
rtx syndrome = gen_reg_rtx (Xmode); |
| 344 |
do_th_tstnbz2 (diff, diff); |
| 345 |
do_one_cmpl2 (diff, diff); |
| 346 |
do_ior3 (syndrome, orc1, diff); |
| 347 |
/* Count the number of equal bits from the beginning of the word. */ |
| 348 |
do_th_rev2 (syndrome, syndrome); |
| 349 |
do_clz2 (shift, syndrome); |
| 350 |
} |
| 351 |
|
| 352 |
do_bswap2 (data1, data1); |
| 353 |
do_bswap2 (data2, data2); |
| 354 |
|
| 355 |
/* The most-significant-non-zero bit of the syndrome marks either the |
| 356 |
first bit that is different, or the top bit of the first zero byte. |
| 357 |
Shifting left now will bring the critical information into the |
| 358 |
top bits. */ |
| 359 |
do_ashl3 (data1, data1, gen_lowpart (QImode, shift)); |
| 360 |
do_ashl3 (data2, data2, gen_lowpart (QImode, shift)); |
| 361 |
|
| 362 |
/* But we need to zero-extend (char is unsigned) the value and then |
| 363 |
perform a signed 32-bit subtraction. */ |
| 364 |
unsigned int shiftr = (xlen - 1) * BITS_PER_UNIT; |
| 365 |
do_lshr3 (data1, data1, GEN_INT (shiftr)); |
| 366 |
do_lshr3 (data2, data2, GEN_INT (shiftr)); |
| 367 |
do_sub3 (result, data1, data2); |
| 368 |
} |
| 369 |
|
| 370 |
/* Expand str(n)cmp using Zbb/TheadBb instructions. |
| 371 |
|
| 372 |
The result will be stored in RESULT. |
| 373 |
The strings are referenced by SRC1 and SRC2. |
| 374 |
The number of bytes to compare is defined by NBYTES. |
| 375 |
The alignment is defined by ALIGNMENT. |
| 376 |
If NCOMPARE is false then libc's strcmp() will be called if comparing |
| 377 |
NBYTES of both strings did not find differences or NULL-bytes. |
| 378 |
|
| 379 |
Return true if expansion was successful, or false otherwise. */ |
| 380 |
|
| 381 |
static bool |
| 382 |
riscv_expand_strcmp_scalar (rtx result, rtx src1, rtx src2, |
| 383 |
unsigned HOST_WIDE_INT nbytes, |
| 384 |
unsigned HOST_WIDE_INT alignment, |
| 385 |
bool ncompare) |
| 386 |
{ |
| 387 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 388 |
|
| 389 |
gcc_assert (TARGET_ZBB || TARGET_XTHEADBB); |
| 390 |
gcc_assert (nbytes > 0); |
| 391 |
gcc_assert ((int)nbytes <= riscv_strcmp_inline_limit); |
| 392 |
gcc_assert (ncompare || (nbytes & (xlen - 1)) == 0); |
| 393 |
|
| 394 |
/* Limit to 12-bits (maximum load-offset). */ |
| 395 |
if (nbytes > IMM_REACH) |
| 396 |
nbytes = IMM_REACH; |
| 397 |
|
| 398 |
/* We don't support big endian. */ |
| 399 |
if (BYTES_BIG_ENDIAN) |
| 400 |
return false; |
| 401 |
|
| 402 |
/* We need xlen-aligned strings. */ |
| 403 |
if (alignment < xlen) |
| 404 |
return false; |
| 405 |
|
| 406 |
/* Overall structure of emitted code: |
| 407 |
Load-and-compare: |
| 408 |
- Load data1 and data2 |
| 409 |
- Set orc1 := orc.b (data1) (or th.tstnbz) |
| 410 |
- Compare strings and either: |
| 411 |
- Fall-through on equality |
| 412 |
- Jump to nonul_end_label if data1 !or end_label |
| 413 |
- Calculate result value and jump to final_label |
| 414 |
// Fall-through |
| 415 |
Call-to-libc or set result to 0 (depending on ncompare) |
| 416 |
Jump to final_label |
| 417 |
nonul_end_label: // words don't match, and no null byte in first word. |
| 418 |
Calculate result value with the use of data1, data2 and orc1 |
| 419 |
Jump to final_label |
| 420 |
end_label: |
| 421 |
Calculate result value with the use of data1, data2 and orc1 |
| 422 |
Jump to final_label |
| 423 |
final_label: |
| 424 |
// Nothing. */ |
| 425 |
|
| 426 |
rtx data1 = gen_reg_rtx (Xmode); |
| 427 |
rtx data2 = gen_reg_rtx (Xmode); |
| 428 |
rtx orc1 = gen_reg_rtx (Xmode); |
| 429 |
rtx nonul_end_label = gen_label_rtx (); |
| 430 |
rtx end_label = gen_label_rtx (); |
| 431 |
rtx final_label = gen_label_rtx (); |
| 432 |
|
| 433 |
/* Generate a sequence of zbb instructions to compare out |
| 434 |
to the length specified. */ |
| 435 |
emit_strcmp_scalar_load_and_compare (result, src1, src2, nbytes, |
| 436 |
data1, data2, orc1, |
| 437 |
end_label, nonul_end_label, final_label); |
| 438 |
|
| 439 |
/* All compared and everything was equal. */ |
| 440 |
if (ncompare) |
| 441 |
{ |
| 442 |
emit_insn (gen_rtx_SET (result, CONST0_RTX (GET_MODE (result)))); |
| 443 |
emit_jump_insn (gen_jump (final_label)); |
| 444 |
emit_barrier (); /* No fall-through. */ |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
emit_strcmp_scalar_call_to_libc (result, src1, src2, nbytes); |
| 449 |
emit_jump_insn (gen_jump (final_label)); |
| 450 |
emit_barrier (); /* No fall-through. */ |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
emit_label (nonul_end_label); |
| 455 |
emit_strcmp_scalar_result_calculation_nonul (result, data1, data2); |
| 456 |
emit_jump_insn (gen_jump (final_label)); |
| 457 |
emit_barrier (); /* No fall-through. */ |
| 458 |
|
| 459 |
emit_label (end_label); |
| 460 |
emit_strcmp_scalar_result_calculation (result, data1, data2, orc1); |
| 461 |
emit_jump_insn (gen_jump (final_label)); |
| 462 |
emit_barrier (); /* No fall-through. */ |
| 463 |
|
| 464 |
emit_label (final_label); |
| 465 |
return true; |
| 466 |
} |
| 467 |
|
| 468 |
/* Expand a string compare operation. |
| 469 |
|
| 470 |
The result will be stored in RESULT. |
| 471 |
The strings are referenced by SRC1 and SRC2. |
| 472 |
The argument BYTES_RTX either holds the number of characters to |
| 473 |
compare, or is NULL_RTX. The argument ALIGN_RTX holds the alignment. |
| 474 |
|
| 475 |
Return true if expansion was successful, or false otherwise. */ |
| 476 |
|
| 477 |
bool |
| 478 |
riscv_expand_strcmp (rtx result, rtx src1, rtx src2, |
| 479 |
rtx bytes_rtx, rtx align_rtx) |
| 480 |
{ |
| 481 |
unsigned HOST_WIDE_INT compare_max; |
| 482 |
unsigned HOST_WIDE_INT nbytes; |
| 483 |
unsigned HOST_WIDE_INT alignment; |
| 484 |
bool ncompare = bytes_rtx != NULL_RTX; |
| 485 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 486 |
|
| 487 |
if (riscv_strcmp_inline_limit == 0) |
| 488 |
return false; |
| 489 |
|
| 490 |
/* Round down the comparision limit to a multiple of xlen. */ |
| 491 |
compare_max = riscv_strcmp_inline_limit & ~(xlen - 1); |
| 492 |
|
| 493 |
/* Decide how many bytes to compare inline. */ |
| 494 |
if (bytes_rtx == NULL_RTX) |
| 495 |
{ |
| 496 |
nbytes = compare_max; |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
/* If we have a length, it must be constant. */ |
| 501 |
if (!CONST_INT_P (bytes_rtx)) |
| 502 |
return false; |
| 503 |
nbytes = UINTVAL (bytes_rtx); |
| 504 |
|
| 505 |
/* If NBYTES is zero the result of strncmp will always be zero, |
| 506 |
but that would require special casing in the caller. So for |
| 507 |
now just don't do an inline expansion. This probably rarely |
| 508 |
happens in practice, but it is tested by the testsuite. */ |
| 509 |
if (nbytes == 0) |
| 510 |
return false; |
| 511 |
|
| 512 |
/* We don't emit parts of a strncmp() call. */ |
| 513 |
if (nbytes > compare_max) |
| 514 |
return false; |
| 515 |
} |
| 516 |
|
| 517 |
/* Guarantees: |
| 518 |
- nbytes > 0 |
| 519 |
- nbytes <= riscv_strcmp_inline_limit |
| 520 |
- nbytes is a multiple of xlen if !ncompare */ |
| 521 |
|
| 522 |
if (!CONST_INT_P (align_rtx)) |
| 523 |
return false; |
| 524 |
alignment = UINTVAL (align_rtx); |
| 525 |
|
| 526 |
if (TARGET_VECTOR && stringop_strategy & STRATEGY_VECTOR) |
| 527 |
{ |
| 528 |
bool ok = riscv_vector::expand_strcmp (result, src1, src2, |
| 529 |
bytes_rtx, alignment, |
| 530 |
ncompare); |
| 531 |
if (ok) |
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
if ((TARGET_ZBB || TARGET_XTHEADBB) && stringop_strategy & STRATEGY_SCALAR) |
| 536 |
return riscv_expand_strcmp_scalar (result, src1, src2, nbytes, alignment, |
| 537 |
ncompare); |
| 538 |
|
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
/* If the provided string is aligned, then read XLEN bytes |
| 543 |
in a loop and use orc.b to find NUL-bytes. */ |
| 544 |
|
| 545 |
static bool |
| 546 |
riscv_expand_strlen_scalar (rtx result, rtx src, rtx align) |
| 547 |
{ |
| 548 |
rtx testval, addr, addr_plus_regsz, word, zeros; |
| 549 |
rtx loop_label, cond; |
| 550 |
|
| 551 |
gcc_assert (TARGET_ZBB || TARGET_XTHEADBB); |
| 552 |
|
| 553 |
/* The alignment needs to be known and big enough. */ |
| 554 |
if (!CONST_INT_P (align) || UINTVAL (align) < GET_MODE_SIZE (Xmode)) |
| 555 |
return false; |
| 556 |
|
| 557 |
testval = gen_reg_rtx (Xmode); |
| 558 |
addr = copy_addr_to_reg (XEXP (src, 0)); |
| 559 |
addr_plus_regsz = gen_reg_rtx (Pmode); |
| 560 |
word = gen_reg_rtx (Xmode); |
| 561 |
zeros = gen_reg_rtx (Xmode); |
| 562 |
|
| 563 |
if (TARGET_ZBB) |
| 564 |
emit_insn (gen_rtx_SET (testval, constm1_rtx)); |
| 565 |
else |
| 566 |
emit_insn (gen_rtx_SET (testval, const0_rtx)); |
| 567 |
|
| 568 |
do_add3 (addr_plus_regsz, addr, GEN_INT (UNITS_PER_WORD)); |
| 569 |
|
| 570 |
loop_label = gen_label_rtx (); |
| 571 |
emit_label (loop_label); |
| 572 |
|
| 573 |
/* Load a word and use orc.b/th.tstnbz to find a zero-byte. */ |
| 574 |
do_load_from_addr (Xmode, word, addr, src); |
| 575 |
do_add3 (addr, addr, GEN_INT (UNITS_PER_WORD)); |
| 576 |
if (TARGET_ZBB) |
| 577 |
do_orcb2 (word, word); |
| 578 |
else |
| 579 |
do_th_tstnbz2 (word, word); |
| 580 |
cond = gen_rtx_EQ (VOIDmode, word, testval); |
| 581 |
emit_unlikely_jump_insn (gen_cbranch4 (Xmode, cond, word, testval, loop_label)); |
| 582 |
|
| 583 |
/* Calculate the return value by counting zero-bits. */ |
| 584 |
if (TARGET_ZBB) |
| 585 |
do_one_cmpl2 (word, word); |
| 586 |
if (TARGET_BIG_ENDIAN) |
| 587 |
do_clz2 (zeros, word); |
| 588 |
else if (TARGET_ZBB) |
| 589 |
do_ctz2 (zeros, word); |
| 590 |
else |
| 591 |
{ |
| 592 |
do_th_rev2 (word, word); |
| 593 |
do_clz2 (zeros, word); |
| 594 |
} |
| 595 |
|
| 596 |
do_lshr3 (zeros, zeros, GEN_INT (exact_log2 (BITS_PER_UNIT))); |
| 597 |
do_add3 (addr, addr, zeros); |
| 598 |
do_sub3 (result, addr, addr_plus_regsz); |
| 599 |
|
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
/* Expand a strlen operation and return true if successful. |
| 604 |
Return false if we should let the compiler generate normal |
| 605 |
code, probably a strlen call. */ |
| 606 |
|
| 607 |
bool |
| 608 |
riscv_expand_strlen (rtx result, rtx src, rtx search_char, rtx align) |
| 609 |
{ |
| 610 |
if (TARGET_VECTOR && stringop_strategy & STRATEGY_VECTOR) |
| 611 |
{ |
| 612 |
riscv_vector::expand_rawmemchr (E_QImode, result, src, search_char, |
| 613 |
/* strlen */ true); |
| 614 |
return true; |
| 615 |
} |
| 616 |
|
| 617 |
gcc_assert (search_char == const0_rtx); |
| 618 |
|
| 619 |
if ((TARGET_ZBB || TARGET_XTHEADBB) && stringop_strategy & STRATEGY_SCALAR) |
| 620 |
return riscv_expand_strlen_scalar (result, src, align); |
| 621 |
|
| 622 |
return false; |
| 623 |
} |
| 624 |
|
| 625 |
/* Generate the sequence of load and compares for memcmp using Zbb. |
| 626 |
|
| 627 |
RESULT is the register where the return value of memcmp will be stored. |
| 628 |
The source pointers are SRC1 and SRC2 (NBYTES bytes to compare). |
| 629 |
DATA1 and DATA2 are registers where the data chunks will be stored. |
| 630 |
DIFF_LABEL is the location of the code that calculates the return value. |
| 631 |
FINAL_LABEL is the location of the code that comes after the calculation |
| 632 |
of the return value. */ |
| 633 |
|
| 634 |
static void |
| 635 |
emit_memcmp_scalar_load_and_compare (rtx result, rtx src1, rtx src2, |
| 636 |
unsigned HOST_WIDE_INT nbytes, |
| 637 |
rtx data1, rtx data2, |
| 638 |
rtx diff_label, rtx final_label) |
| 639 |
{ |
| 640 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 641 |
unsigned HOST_WIDE_INT offset = 0; |
| 642 |
|
| 643 |
while (nbytes > 0) |
| 644 |
{ |
| 645 |
unsigned HOST_WIDE_INT cmp_bytes = xlen < nbytes ? xlen : nbytes; |
| 646 |
machine_mode load_mode; |
| 647 |
|
| 648 |
/* Special cases to avoid masking of trailing bytes. */ |
| 649 |
if (cmp_bytes == 1) |
| 650 |
load_mode = QImode; |
| 651 |
else if (cmp_bytes == 2) |
| 652 |
load_mode = HImode; |
| 653 |
else if (cmp_bytes == 4) |
| 654 |
load_mode = SImode; |
| 655 |
else |
| 656 |
load_mode = Xmode; |
| 657 |
|
| 658 |
rtx addr1 = adjust_address (src1, load_mode, offset); |
| 659 |
do_load (load_mode, data1, addr1); |
| 660 |
rtx addr2 = adjust_address (src2, load_mode, offset); |
| 661 |
do_load (load_mode, data2, addr2); |
| 662 |
|
| 663 |
/* Fast-path for a single byte. */ |
| 664 |
if (cmp_bytes == 1) |
| 665 |
{ |
| 666 |
do_sub3 (result, data1, data2); |
| 667 |
emit_jump_insn (gen_jump (final_label)); |
| 668 |
emit_barrier (); /* No fall-through. */ |
| 669 |
return; |
| 670 |
} |
| 671 |
|
| 672 |
/* Shift off trailing bytes in words if needed. */ |
| 673 |
unsigned int load_bytes = GET_MODE_SIZE (load_mode).to_constant (); |
| 674 |
if (cmp_bytes < load_bytes) |
| 675 |
{ |
| 676 |
int shamt = (load_bytes - cmp_bytes) * BITS_PER_UNIT; |
| 677 |
do_ashl3 (data1, data1, GEN_INT (shamt)); |
| 678 |
do_ashl3 (data2, data2, GEN_INT (shamt)); |
| 679 |
} |
| 680 |
|
| 681 |
/* Break out if data1 != data2 */ |
| 682 |
rtx cond = gen_rtx_NE (VOIDmode, data1, data2); |
| 683 |
emit_unlikely_jump_insn (gen_cbranch4 (Pmode, cond, data1, |
| 684 |
data2, diff_label)); |
| 685 |
/* Fall-through on equality. */ |
| 686 |
|
| 687 |
offset += cmp_bytes; |
| 688 |
nbytes -= cmp_bytes; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
/* memcmp result calculation. |
| 693 |
|
| 694 |
RESULT is the register where the return value will be stored. |
| 695 |
The two data chunks are in DATA1 and DATA2. */ |
| 696 |
|
| 697 |
static void |
| 698 |
emit_memcmp_scalar_result_calculation (rtx result, rtx data1, rtx data2) |
| 699 |
{ |
| 700 |
/* Get bytes in big-endian order and compare as words. */ |
| 701 |
do_bswap2 (data1, data1); |
| 702 |
do_bswap2 (data2, data2); |
| 703 |
|
| 704 |
/* Synthesize (data1 >= data2) ? 1 : -1 in a branchless sequence. */ |
| 705 |
emit_insn (gen_slt_3 (LTU, Xmode, Xmode, result, data1, data2)); |
| 706 |
do_neg2 (result, result); |
| 707 |
do_ior3 (result, result, const1_rtx); |
| 708 |
} |
| 709 |
|
| 710 |
/* Expand memcmp using scalar instructions (incl. Zbb). |
| 711 |
|
| 712 |
RESULT is the register where the return value will be stored. |
| 713 |
The source pointers are SRC1 and SRC2 (NBYTES bytes to compare). */ |
| 714 |
|
| 715 |
static bool |
| 716 |
riscv_expand_block_compare_scalar (rtx result, rtx src1, rtx src2, rtx nbytes) |
| 717 |
{ |
| 718 |
const unsigned HOST_WIDE_INT xlen = GET_MODE_SIZE (Xmode); |
| 719 |
|
| 720 |
if (optimize_function_for_size_p (cfun)) |
| 721 |
return false; |
| 722 |
|
| 723 |
/* We don't support big endian. */ |
| 724 |
if (BYTES_BIG_ENDIAN) |
| 725 |
return false; |
| 726 |
|
| 727 |
if (!CONST_INT_P (nbytes)) |
| 728 |
return false; |
| 729 |
|
| 730 |
/* We need the rev (bswap) instruction. */ |
| 731 |
if (!TARGET_ZBB) |
| 732 |
return false; |
| 733 |
|
| 734 |
unsigned HOST_WIDE_INT length = UINTVAL (nbytes); |
| 735 |
|
| 736 |
/* Limit to 12-bits (maximum load-offset). */ |
| 737 |
if (length > IMM_REACH) |
| 738 |
length = IMM_REACH; |
| 739 |
|
| 740 |
/* We need xlen-aligned memory. */ |
| 741 |
unsigned HOST_WIDE_INT align = MIN (MEM_ALIGN (src1), MEM_ALIGN (src2)); |
| 742 |
if (align < (xlen * BITS_PER_UNIT)) |
| 743 |
return false; |
| 744 |
|
| 745 |
if (length > RISCV_MAX_MOVE_BYTES_STRAIGHT) |
| 746 |
return false; |
| 747 |
|
| 748 |
/* Overall structure of emitted code: |
| 749 |
Load-and-compare: |
| 750 |
- Load data1 and data2 |
| 751 |
- Compare strings and either: |
| 752 |
- Fall-through on equality |
| 753 |
- Jump to end_label if data1 != data2 |
| 754 |
// Fall-through |
| 755 |
Set result to 0 and jump to final_label |
| 756 |
diff_label: |
| 757 |
Calculate result value with the use of data1 and data2 |
| 758 |
Jump to final_label |
| 759 |
final_label: |
| 760 |
// Nothing. */ |
| 761 |
|
| 762 |
rtx data1 = gen_reg_rtx (Xmode); |
| 763 |
rtx data2 = gen_reg_rtx (Xmode); |
| 764 |
rtx diff_label = gen_label_rtx (); |
| 765 |
rtx final_label = gen_label_rtx (); |
| 766 |
|
| 767 |
/* Generate a sequence of zbb instructions to compare out |
| 768 |
to the length specified. */ |
| 769 |
emit_memcmp_scalar_load_and_compare (result, src1, src2, length, |
| 770 |
data1, data2, |
| 771 |
diff_label, final_label); |
| 772 |
|
| 773 |
emit_move_insn (result, CONST0_RTX (GET_MODE (result))); |
| 774 |
emit_jump_insn (gen_jump (final_label)); |
| 775 |
emit_barrier (); /* No fall-through. */ |
| 776 |
|
| 777 |
emit_label (diff_label); |
| 778 |
emit_memcmp_scalar_result_calculation (result, data1, data2); |
| 779 |
emit_jump_insn (gen_jump (final_label)); |
| 780 |
emit_barrier (); /* No fall-through. */ |
| 781 |
|
| 782 |
emit_label (final_label); |
| 783 |
return true; |
| 784 |
} |
| 785 |
|
| 786 |
/* Expand memcmp operation. |
| 787 |
|
| 788 |
RESULT is the register where the return value will be stored. |
| 789 |
The source pointers are SRC1 and SRC2 (NBYTES bytes to compare). */ |
| 790 |
|
| 791 |
bool |
| 792 |
riscv_expand_block_compare (rtx result, rtx src1, rtx src2, rtx nbytes) |
| 793 |
{ |
| 794 |
if (stringop_strategy & STRATEGY_SCALAR) |
| 795 |
return riscv_expand_block_compare_scalar (result, src1, src2, nbytes); |
| 796 |
|
| 797 |
return false; |
| 798 |
} |
| 799 |
/* Emit straight-line code to move LENGTH bytes from SRC to DEST |
| 800 |
with accesses that are ALIGN bytes aligned. |
| 801 |
Assume that the areas do not overlap. */ |
| 802 |
|
| 803 |
static void |
| 804 |
riscv_block_move_straight (rtx dest, rtx src, unsigned HOST_WIDE_INT length, |
| 805 |
unsigned HOST_WIDE_INT align) |
| 806 |
{ |
| 807 |
unsigned HOST_WIDE_INT offset = 0, delta; |
| 808 |
unsigned HOST_WIDE_INT bits; |
| 809 |
int i; |
| 810 |
enum machine_mode mode; |
| 811 |
rtx *regs; |
| 812 |
|
| 813 |
bits = MAX (BITS_PER_UNIT, MIN (BITS_PER_WORD, align)); |
| 814 |
|
| 815 |
mode = mode_for_size (bits, MODE_INT, 0).require (); |
| 816 |
delta = bits / BITS_PER_UNIT; |
| 817 |
|
| 818 |
if (2 * delta <= length) |
| 819 |
{ |
| 820 |
/* Allocate a buffer for the temporary registers. */ |
| 821 |
regs = XALLOCAVEC (rtx, length / delta - 1); |
| 822 |
|
| 823 |
/* Load as many BITS-sized chunks as possible. Use a normal load if |
| 824 |
the source has enough alignment, otherwise use left/right pairs. */ |
| 825 |
for (offset = 0, i = 0; offset + 2 * delta <= length; |
| 826 |
offset += delta, i++) |
| 827 |
{ |
| 828 |
regs[i] = gen_reg_rtx (mode); |
| 829 |
riscv_emit_move (regs[i], adjust_address (src, mode, offset)); |
| 830 |
} |
| 831 |
|
| 832 |
/* Copy the chunks to the destination. */ |
| 833 |
for (offset = 0, i = 0; offset + 2 * delta <= length; |
| 834 |
offset += delta, i++) |
| 835 |
riscv_emit_move (adjust_address (dest, mode, offset), regs[i]); |
| 836 |
} |
| 837 |
|
| 838 |
/* Mop up any left-over bytes. */ |
| 839 |
if (offset < length) |
| 840 |
{ |
| 841 |
src = adjust_address (src, BLKmode, offset); |
| 842 |
dest = adjust_address (dest, BLKmode, offset); |
| 843 |
move_by_pieces (dest, src, length - offset, align, RETURN_BEGIN); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
/* Helper function for doing a loop-based block operation on memory |
| 848 |
reference MEM. |
| 849 |
|
| 850 |
Create a new base register for use within the loop and point it to |
| 851 |
the start of MEM. Create a new memory reference that uses this |
| 852 |
register and has an alignment of ALIGN. Store them in *LOOP_REG |
| 853 |
and *LOOP_MEM respectively. */ |
| 854 |
|
| 855 |
static void |
| 856 |
riscv_adjust_block_mem (rtx mem, unsigned HOST_WIDE_INT align, |
| 857 |
rtx *loop_reg, rtx *loop_mem) |
| 858 |
{ |
| 859 |
*loop_reg = copy_addr_to_reg (XEXP (mem, 0)); |
| 860 |
|
| 861 |
/* Although the new mem does not refer to a known location, |
| 862 |
it does keep up to LENGTH bytes of alignment. */ |
| 863 |
*loop_mem = change_address (mem, BLKmode, *loop_reg); |
| 864 |
set_mem_align (*loop_mem, align); |
| 865 |
} |
| 866 |
|
| 867 |
/* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER |
| 868 |
bytes at a time. LENGTH must be at least BYTES_PER_ITER. The alignment |
| 869 |
of the access can be set by ALIGN. Assume that the memory regions do not |
| 870 |
overlap. */ |
| 871 |
|
| 872 |
static void |
| 873 |
riscv_block_move_loop (rtx dest, rtx src, unsigned HOST_WIDE_INT length, |
| 874 |
unsigned HOST_WIDE_INT align, |
| 875 |
unsigned HOST_WIDE_INT bytes_per_iter) |
| 876 |
{ |
| 877 |
rtx label, src_reg, dest_reg, final_src, test; |
| 878 |
unsigned HOST_WIDE_INT leftover; |
| 879 |
|
| 880 |
leftover = length % bytes_per_iter; |
| 881 |
length -= leftover; |
| 882 |
|
| 883 |
/* Create registers and memory references for use within the loop. */ |
| 884 |
riscv_adjust_block_mem (src, align, &src_reg, &src); |
| 885 |
riscv_adjust_block_mem (dest, align, &dest_reg, &dest); |
| 886 |
|
| 887 |
/* Calculate the value that SRC_REG should have after the last iteration |
| 888 |
of the loop. */ |
| 889 |
final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length), |
| 890 |
0, 0, OPTAB_WIDEN); |
| 891 |
|
| 892 |
/* Emit the start of the loop. */ |
| 893 |
label = gen_label_rtx (); |
| 894 |
emit_label (label); |
| 895 |
|
| 896 |
/* Emit the loop body. */ |
| 897 |
riscv_block_move_straight (dest, src, bytes_per_iter, align); |
| 898 |
|
| 899 |
/* Move on to the next block. */ |
| 900 |
riscv_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter)); |
| 901 |
riscv_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter)); |
| 902 |
|
| 903 |
/* Emit the loop condition. */ |
| 904 |
test = gen_rtx_NE (VOIDmode, src_reg, final_src); |
| 905 |
emit_jump_insn (gen_cbranch4 (Pmode, test, src_reg, final_src, label)); |
| 906 |
|
| 907 |
/* Mop up any left-over bytes. */ |
| 908 |
if (leftover) |
| 909 |
riscv_block_move_straight (dest, src, leftover, align); |
| 910 |
else |
| 911 |
emit_insn(gen_nop ()); |
| 912 |
} |
| 913 |
|
| 914 |
/* Expand a cpymemsi instruction, which copies LENGTH bytes from |
| 915 |
memory reference SRC to memory reference DEST. */ |
| 916 |
|
| 917 |
static bool |
| 918 |
riscv_expand_block_move_scalar (rtx dest, rtx src, rtx length) |
| 919 |
{ |
| 920 |
if (!CONST_INT_P (length)) |
| 921 |
return false; |
| 922 |
|
| 923 |
unsigned HOST_WIDE_INT hwi_length = UINTVAL (length); |
| 924 |
unsigned HOST_WIDE_INT factor, align; |
| 925 |
|
| 926 |
if (riscv_slow_unaligned_access_p) |
| 927 |
{ |
| 928 |
align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD); |
| 929 |
factor = BITS_PER_WORD / align; |
| 930 |
} |
| 931 |
else |
| 932 |
{ |
| 933 |
/* Pretend word-alignment. */ |
| 934 |
align = BITS_PER_WORD; |
| 935 |
factor = 1; |
| 936 |
} |
| 937 |
|
| 938 |
if (optimize_function_for_size_p (cfun) |
| 939 |
&& hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false)) |
| 940 |
return false; |
| 941 |
|
| 942 |
if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)) |
| 943 |
{ |
| 944 |
riscv_block_move_straight (dest, src, hwi_length, align); |
| 945 |
return true; |
| 946 |
} |
| 947 |
else if (optimize && align >= BITS_PER_WORD) |
| 948 |
{ |
| 949 |
unsigned min_iter_words |
| 950 |
= RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD; |
| 951 |
unsigned iter_words = min_iter_words; |
| 952 |
unsigned HOST_WIDE_INT bytes = hwi_length; |
| 953 |
unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD; |
| 954 |
|
| 955 |
/* Lengthen the loop body if it shortens the tail. */ |
| 956 |
for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++) |
| 957 |
{ |
| 958 |
unsigned cur_cost = iter_words + words % iter_words; |
| 959 |
unsigned new_cost = i + words % i; |
| 960 |
if (new_cost <= cur_cost) |
| 961 |
iter_words = i; |
| 962 |
} |
| 963 |
|
| 964 |
riscv_block_move_loop (dest, src, bytes, align, |
| 965 |
iter_words * UNITS_PER_WORD); |
| 966 |
return true; |
| 967 |
} |
| 968 |
|
| 969 |
return false; |
| 970 |
} |
| 971 |
|
| 972 |
/* This function delegates block-move expansion to either the vector |
| 973 |
implementation or the scalar one. Return TRUE if successful or FALSE |
| 974 |
otherwise. Assume that the memory regions do not overlap. */ |
| 975 |
|
| 976 |
bool |
| 977 |
riscv_expand_block_move (rtx dest, rtx src, rtx length) |
| 978 |
{ |
| 979 |
if (TARGET_VECTOR |
| 980 |
&& stringop_strategy & STRATEGY_VECTOR) |
| 981 |
{ |
| 982 |
bool ok = riscv_vector::expand_block_move (dest, src, length, false); |
| 983 |
if (ok) |
| 984 |
return true; |
| 985 |
} |
| 986 |
|
| 987 |
if (stringop_strategy & STRATEGY_SCALAR) |
| 988 |
return riscv_expand_block_move_scalar (dest, src, length); |
| 989 |
|
| 990 |
return false; |
| 991 |
} |
| 992 |
|
| 993 |
/* Expand a block-clear instruction via cbo.zero instructions. */ |
| 994 |
|
| 995 |
static bool |
| 996 |
riscv_expand_block_clear_zicboz_zic64b (rtx dest, rtx length) |
| 997 |
{ |
| 998 |
unsigned HOST_WIDE_INT hwi_length; |
| 999 |
unsigned HOST_WIDE_INT align; |
| 1000 |
const unsigned HOST_WIDE_INT cbo_bytes = 64; |
| 1001 |
|
| 1002 |
gcc_assert (TARGET_ZICBOZ && TARGET_ZIC64B); |
| 1003 |
|
| 1004 |
if (!CONST_INT_P (length)) |
| 1005 |
return false; |
| 1006 |
|
| 1007 |
hwi_length = UINTVAL (length); |
| 1008 |
if (hwi_length < cbo_bytes) |
| 1009 |
return false; |
| 1010 |
|
| 1011 |
align = MEM_ALIGN (dest) / BITS_PER_UNIT; |
| 1012 |
if (align < cbo_bytes) |
| 1013 |
return false; |
| 1014 |
|
| 1015 |
/* We don't emit loops. Instead apply move-bytes limitation. */ |
| 1016 |
unsigned HOST_WIDE_INT max_bytes = RISCV_MAX_MOVE_BYTES_STRAIGHT / |
| 1017 |
UNITS_PER_WORD * cbo_bytes; |
| 1018 |
if (hwi_length > max_bytes) |
| 1019 |
return false; |
| 1020 |
|
| 1021 |
unsigned HOST_WIDE_INT offset = 0; |
| 1022 |
while (offset + cbo_bytes <= hwi_length) |
| 1023 |
{ |
| 1024 |
rtx mem = adjust_address (dest, BLKmode, offset); |
| 1025 |
rtx addr = force_reg (Pmode, XEXP (mem, 0)); |
| 1026 |
if (TARGET_64BIT) |
| 1027 |
emit_insn (gen_riscv_zero_di (addr)); |
| 1028 |
else |
| 1029 |
emit_insn (gen_riscv_zero_si (addr)); |
| 1030 |
offset += cbo_bytes; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if (offset < hwi_length) |
| 1034 |
{ |
| 1035 |
rtx mem = adjust_address (dest, BLKmode, offset); |
| 1036 |
clear_by_pieces (mem, hwi_length - offset, align); |
| 1037 |
} |
| 1038 |
|
| 1039 |
return true; |
| 1040 |
} |
| 1041 |
|
| 1042 |
bool |
| 1043 |
riscv_expand_block_clear (rtx dest, rtx length) |
| 1044 |
{ |
| 1045 |
/* Only use setmem-zero expansion for Zicboz + Zic64b. */ |
| 1046 |
if (!TARGET_ZICBOZ || !TARGET_ZIC64B) |
| 1047 |
return false; |
| 1048 |
|
| 1049 |
if (optimize_function_for_size_p (cfun)) |
| 1050 |
return false; |
| 1051 |
|
| 1052 |
return riscv_expand_block_clear_zicboz_zic64b (dest, length); |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* --- Vector expanders --- */ |
| 1056 |
|
| 1057 |
namespace riscv_vector { |
| 1058 |
|
| 1059 |
struct stringop_info { |
| 1060 |
rtx avl; |
| 1061 |
bool need_loop; |
| 1062 |
machine_mode vmode; |
| 1063 |
}; |
| 1064 |
|
| 1065 |
/* If a vectorized stringop should be used populate INFO and return TRUE. |
| 1066 |
Otherwise return false and leave INFO unchanged. |
| 1067 |
|
| 1068 |
MAX_EW is the maximum element width that the caller wants to use and |
| 1069 |
LENGTH_IN is the length of the stringop in bytes. |
| 1070 |
|
| 1071 |
This is currently used for cpymem and setmem. If expand_vec_cmpmem switches |
| 1072 |
to using it too then check_vectorise_memory_operation can be removed. |
| 1073 |
*/ |
| 1074 |
|
| 1075 |
static bool |
| 1076 |
use_vector_stringop_p (struct stringop_info &info, HOST_WIDE_INT max_ew, |
| 1077 |
rtx length_in) |
| 1078 |
{ |
| 1079 |
bool need_loop = true; |
| 1080 |
machine_mode vmode = VOIDmode; |
| 1081 |
/* The number of elements in the stringop. */ |
| 1082 |
rtx avl = length_in; |
| 1083 |
HOST_WIDE_INT potential_ew = max_ew; |
| 1084 |
|
| 1085 |
if (!TARGET_VECTOR |
| 1086 |
|| !(stringop_strategy & STRATEGY_VECTOR)) |
| 1087 |
return false; |
| 1088 |
|
| 1089 |
if (TARGET_XTHEADVECTOR |
| 1090 |
&& (!CONST_INT_P (length_in) |
| 1091 |
|| known_lt (INTVAL (length_in), BYTES_PER_RISCV_VECTOR))) |
| 1092 |
return false; |
| 1093 |
|
| 1094 |
if (CONST_INT_P (length_in)) |
| 1095 |
{ |
| 1096 |
HOST_WIDE_INT length = INTVAL (length_in); |
| 1097 |
|
| 1098 |
/* If the VLEN and preferred LMUL allow the entire block to be copied in |
| 1099 |
one go then no loop is needed. */ |
| 1100 |
if (known_le (length, BYTES_PER_RISCV_VECTOR * TARGET_MAX_LMUL)) |
| 1101 |
{ |
| 1102 |
need_loop = false; |
| 1103 |
|
| 1104 |
/* If a single scalar load / store pair can do the job, leave it |
| 1105 |
to the scalar code to do that. */ |
| 1106 |
/* ??? If fast unaligned access is supported, the scalar code could |
| 1107 |
use suitably sized scalars irrespective of alignment. If that |
| 1108 |
gets fixed, we have to adjust the test here. */ |
| 1109 |
|
| 1110 |
if (pow2p_hwi (length) && length <= potential_ew) |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Find the vector mode to use. Using the largest possible element |
| 1115 |
size is likely to give smaller constants, and thus potentially |
| 1116 |
reducing code size. However, if we need a loop, we need to update |
| 1117 |
the pointers, and that is more complicated with a larger element |
| 1118 |
size, unless we use an immediate, which prevents us from dynamically |
| 1119 |
using the targets transfer size that the hart supports. And then, |
| 1120 |
unless we know the *exact* vector size of the hart, we'd need |
| 1121 |
multiple vsetvli / branch statements, so it's not even a size win. |
| 1122 |
If, in the future, we find an RISCV-V implementation that is slower |
| 1123 |
for small element widths, we might allow larger element widths for |
| 1124 |
loops too. */ |
| 1125 |
if (need_loop) |
| 1126 |
potential_ew = 1; |
| 1127 |
for (; potential_ew; potential_ew >>= 1) |
| 1128 |
{ |
| 1129 |
scalar_int_mode elem_mode; |
| 1130 |
unsigned HOST_WIDE_INT bits = potential_ew * BITS_PER_UNIT; |
| 1131 |
poly_uint64 per_iter; |
| 1132 |
poly_int64 nunits; |
| 1133 |
|
| 1134 |
if (need_loop) |
| 1135 |
per_iter = BYTES_PER_RISCV_VECTOR * TARGET_MAX_LMUL; |
| 1136 |
else |
| 1137 |
per_iter = length; |
| 1138 |
/* BYTES_PER_RISCV_VECTOR * TARGET_MAX_LMUL may not be divisible by |
| 1139 |
this potential_ew. */ |
| 1140 |
if (!multiple_p (per_iter, potential_ew, &nunits)) |
| 1141 |
continue; |
| 1142 |
|
| 1143 |
/* Unless we get an implementation that's slow for small element |
| 1144 |
size / non-word-aligned accesses, we assume that the hardware |
| 1145 |
handles this well, and we don't want to complicate the code |
| 1146 |
with shifting word contents around or handling extra bytes at |
| 1147 |
the start and/or end. So we want the total transfer size and |
| 1148 |
alignment to fit with the element size. */ |
| 1149 |
if (length % potential_ew != 0 |
| 1150 |
|| !int_mode_for_size (bits, 0).exists (&elem_mode)) |
| 1151 |
continue; |
| 1152 |
|
| 1153 |
poly_uint64 mode_units; |
| 1154 |
/* Find the mode to use for the copy inside the loop - or the |
| 1155 |
sole copy, if there is no loop. */ |
| 1156 |
if (!need_loop) |
| 1157 |
{ |
| 1158 |
/* Try if we have an exact mode for the copy. */ |
| 1159 |
if (riscv_vector::get_vector_mode (elem_mode, |
| 1160 |
nunits).exists (&vmode)) |
| 1161 |
break; |
| 1162 |
/* Since we don't have a mode that exactly matches the transfer |
| 1163 |
size, we'll need to use pred_store, which is not available |
| 1164 |
for all vector modes, but only iE_RVV_M* modes, hence trying |
| 1165 |
to find a vector mode for a merely rounded-up size is |
| 1166 |
pointless. |
| 1167 |
Still, by choosing a lower LMUL factor that still allows |
| 1168 |
an entire transfer, we can reduce register pressure. */ |
| 1169 |
for (unsigned lmul = 1; lmul < TARGET_MAX_LMUL; lmul <<= 1) |
| 1170 |
if (known_le (length * BITS_PER_UNIT, TARGET_MIN_VLEN * lmul) |
| 1171 |
&& multiple_p (BYTES_PER_RISCV_VECTOR * lmul, potential_ew, |
| 1172 |
&mode_units) |
| 1173 |
&& (riscv_vector::get_vector_mode |
| 1174 |
(elem_mode, mode_units).exists (&vmode))) |
| 1175 |
break; |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* Stop searching if a suitable vmode has been found. */ |
| 1179 |
if (vmode != VOIDmode) |
| 1180 |
break; |
| 1181 |
|
| 1182 |
/* BYTES_PER_RISCV_VECTOR * TARGET_MAX_LMUL will at least be divisible |
| 1183 |
by potential_ew 1, so this should succeed eventually. */ |
| 1184 |
if (multiple_p (BYTES_PER_RISCV_VECTOR * TARGET_MAX_LMUL, |
| 1185 |
potential_ew, &mode_units) |
| 1186 |
&& riscv_vector::get_vector_mode (elem_mode, |
| 1187 |
mode_units).exists (&vmode)) |
| 1188 |
break; |
| 1189 |
|
| 1190 |
/* We may get here if we tried an element size that's larger than |
| 1191 |
the hardware supports, but we should at least find a suitable |
| 1192 |
byte vector mode. */ |
| 1193 |
gcc_assert (potential_ew > 1); |
| 1194 |
} |
| 1195 |
if (potential_ew > 1) |
| 1196 |
avl = GEN_INT (length / potential_ew); |
| 1197 |
} |
| 1198 |
else |
| 1199 |
{ |
| 1200 |
gcc_assert (get_lmul_mode (QImode, TARGET_MAX_LMUL).exists (&vmode)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
/* A memcpy libcall in the worst case takes 3 instructions to prepare the |
| 1204 |
arguments + 1 for the call. When RVV should take 7 instructions and |
| 1205 |
we're optimizing for size a libcall may be preferable. */ |
| 1206 |
if (optimize_function_for_size_p (cfun) && need_loop) |
| 1207 |
return false; |
| 1208 |
|
| 1209 |
info.need_loop = need_loop; |
| 1210 |
info.vmode = vmode; |
| 1211 |
info.avl = avl; |
| 1212 |
return true; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/* Used by cpymemsi in riscv.md . */ |
| 1216 |
|
| 1217 |
bool |
| 1218 |
expand_block_move (rtx dst_in, rtx src_in, rtx length_in, bool movmem_p) |
| 1219 |
{ |
| 1220 |
/* |
| 1221 |
memcpy: |
| 1222 |
mv a3, a0 # Copy destination |
| 1223 |
loop: |
| 1224 |
vsetvli t0, a2, e8, m8, ta, ma # Vectors of 8b |
| 1225 |
vle8.v v0, (a1) # Load bytes |
| 1226 |
add a1, a1, t0 # Bump pointer |
| 1227 |
sub a2, a2, t0 # Decrement count |
| 1228 |
vse8.v v0, (a3) # Store bytes |
| 1229 |
add a3, a3, t0 # Bump pointer |
| 1230 |
bnez a2, loop # Any more? |
| 1231 |
ret # Return |
| 1232 |
*/ |
| 1233 |
struct stringop_info info; |
| 1234 |
|
| 1235 |
HOST_WIDE_INT potential_ew |
| 1236 |
= (MIN (MIN (MEM_ALIGN (src_in), MEM_ALIGN (dst_in)), BITS_PER_WORD) |
| 1237 |
/ BITS_PER_UNIT); |
| 1238 |
|
| 1239 |
if (!use_vector_stringop_p (info, potential_ew, length_in)) |
| 1240 |
return false; |
| 1241 |
|
| 1242 |
/* Inlining general memmove is a pessimisation: we can't avoid having to |
| 1243 |
decide which direction to go at runtime, which is costly in instruction |
| 1244 |
count however for situations where the entire move fits in one vector |
| 1245 |
operation we can do all reads before doing any writes so we don't have to |
| 1246 |
worry so generate the inline vector code in such situations. */ |
| 1247 |
if (info.need_loop && movmem_p) |
| 1248 |
return false; |
| 1249 |
|
| 1250 |
rtx src, dst; |
| 1251 |
rtx vec; |
| 1252 |
|
| 1253 |
/* avl holds the (remaining) length of the required copy. |
| 1254 |
cnt holds the length we copy with the current load/store pair. */ |
| 1255 |
rtx cnt = info.avl; |
| 1256 |
rtx label = NULL_RTX; |
| 1257 |
rtx dst_addr = copy_addr_to_reg (XEXP (dst_in, 0)); |
| 1258 |
rtx src_addr = copy_addr_to_reg (XEXP (src_in, 0)); |
| 1259 |
|
| 1260 |
if (info.need_loop) |
| 1261 |
{ |
| 1262 |
info.avl = copy_to_mode_reg (Pmode, info.avl); |
| 1263 |
cnt = gen_reg_rtx (Pmode); |
| 1264 |
label = gen_label_rtx (); |
| 1265 |
|
| 1266 |
emit_label (label); |
| 1267 |
emit_insn (riscv_vector::gen_no_side_effects_vsetvl_rtx (info.vmode, cnt, |
| 1268 |
info.avl)); |
| 1269 |
} |
| 1270 |
|
| 1271 |
vec = gen_reg_rtx (info.vmode); |
| 1272 |
src = change_address (src_in, info.vmode, src_addr); |
| 1273 |
dst = change_address (dst_in, info.vmode, dst_addr); |
| 1274 |
|
| 1275 |
/* If we don't need a loop and have a suitable mode to describe the size, |
| 1276 |
just do a load / store pair and leave it up to the later lazy code |
| 1277 |
motion pass to insert the appropriate vsetvli. */ |
| 1278 |
if (!info.need_loop |
| 1279 |
&& known_eq (GET_MODE_SIZE (info.vmode), INTVAL (length_in))) |
| 1280 |
{ |
| 1281 |
emit_move_insn (vec, src); |
| 1282 |
emit_move_insn (dst, vec); |
| 1283 |
} |
| 1284 |
else |
| 1285 |
{ |
| 1286 |
machine_mode mask_mode = riscv_vector::get_vector_mode |
| 1287 |
(BImode, GET_MODE_NUNITS (info.vmode)).require (); |
| 1288 |
rtx mask = CONSTM1_RTX (mask_mode); |
| 1289 |
if (!satisfies_constraint_vl (cnt)) |
| 1290 |
cnt= force_reg (Pmode, cnt); |
| 1291 |
rtx m_ops[] = {vec, mask, src}; |
| 1292 |
emit_nonvlmax_insn (code_for_pred_mov (info.vmode), |
| 1293 |
riscv_vector::UNARY_OP_TAMA, m_ops, cnt); |
| 1294 |
emit_insn (gen_pred_store (info.vmode, dst, mask, vec, cnt, |
| 1295 |
get_avl_type_rtx (riscv_vector::NONVLMAX))); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (info.need_loop) |
| 1299 |
{ |
| 1300 |
emit_insn (gen_rtx_SET (src_addr, gen_rtx_PLUS (Pmode, src_addr, cnt))); |
| 1301 |
emit_insn (gen_rtx_SET (dst_addr, gen_rtx_PLUS (Pmode, dst_addr, cnt))); |
| 1302 |
emit_insn (gen_rtx_SET (info.avl, gen_rtx_MINUS (Pmode, info.avl, cnt))); |
| 1303 |
|
| 1304 |
/* Emit the loop condition. */ |
| 1305 |
rtx test = gen_rtx_NE (VOIDmode, info.avl, const0_rtx); |
| 1306 |
emit_jump_insn (gen_cbranch4 (Pmode, test, info.avl, const0_rtx, label)); |
| 1307 |
emit_insn (gen_nop ()); |
| 1308 |
} |
| 1309 |
|
| 1310 |
return true; |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
/* Implement rawmemchr<mode> and strlen using vector instructions. |
| 1315 |
It can be assumed that the needle is in the haystack, otherwise the |
| 1316 |
behavior is undefined. */ |
| 1317 |
|
| 1318 |
void |
| 1319 |
expand_rawmemchr (machine_mode mode, rtx dst, rtx haystack, rtx needle, |
| 1320 |
bool strlen) |
| 1321 |
{ |
| 1322 |
/* |
| 1323 |
rawmemchr: |
| 1324 |
loop: |
| 1325 |
vsetvli a1, zero, e[8,16,32,64], m1, ta, ma |
| 1326 |
vle[8,16,32,64]ff.v v8, (a0) # Load. |
| 1327 |
csrr a1, vl # Get number of bytes read. |
| 1328 |
vmseq.vx v0, v8, pat # v0 = (v8 == {pat, pat, ...}) |
| 1329 |
vfirst.m a2, v0 # Find first hit. |
| 1330 |
add a0, a0, a1 # Bump pointer. |
| 1331 |
bltz a2, loop # Not found? |
| 1332 |
|
| 1333 |
sub a0, a0, a1 # Go back by a1. |
| 1334 |
shll a2, a2, [0,1,2,3] # Shift to get byte offset. |
| 1335 |
add a0, a0, a2 # Add the offset. |
| 1336 |
|
| 1337 |
ret |
| 1338 |
*/ |
| 1339 |
gcc_assert (TARGET_VECTOR); |
| 1340 |
|
| 1341 |
if (strlen) |
| 1342 |
gcc_assert (mode == E_QImode); |
| 1343 |
|
| 1344 |
unsigned int isize = GET_MODE_SIZE (mode).to_constant (); |
| 1345 |
int lmul = TARGET_MAX_LMUL; |
| 1346 |
poly_int64 nunits = exact_div (BYTES_PER_RISCV_VECTOR * lmul, isize); |
| 1347 |
|
| 1348 |
machine_mode vmode; |
| 1349 |
if (!riscv_vector::get_vector_mode (GET_MODE_INNER (mode), |
| 1350 |
nunits).exists (&vmode)) |
| 1351 |
gcc_unreachable (); |
| 1352 |
|
| 1353 |
machine_mode mask_mode = riscv_vector::get_mask_mode (vmode); |
| 1354 |
|
| 1355 |
rtx cnt = gen_reg_rtx (Pmode); |
| 1356 |
emit_move_insn (cnt, CONST0_RTX (Pmode)); |
| 1357 |
|
| 1358 |
rtx end = gen_reg_rtx (Pmode); |
| 1359 |
rtx vec = gen_reg_rtx (vmode); |
| 1360 |
rtx mask = gen_reg_rtx (mask_mode); |
| 1361 |
|
| 1362 |
/* After finding the first vector element matching the needle, we |
| 1363 |
need to multiply by the vector element width (SEW) in order to |
| 1364 |
return a pointer to the matching byte. */ |
| 1365 |
unsigned int shift = exact_log2 (GET_MODE_SIZE (mode).to_constant ()); |
| 1366 |
|
| 1367 |
rtx src_addr = copy_addr_to_reg (XEXP (haystack, 0)); |
| 1368 |
rtx start_addr = copy_addr_to_reg (XEXP (haystack, 0)); |
| 1369 |
|
| 1370 |
rtx loop = gen_label_rtx (); |
| 1371 |
emit_label (loop); |
| 1372 |
|
| 1373 |
rtx vsrc = change_address (haystack, vmode, src_addr); |
| 1374 |
|
| 1375 |
/* Bump the pointer. */ |
| 1376 |
rtx step = gen_reg_rtx (Pmode); |
| 1377 |
emit_insn (gen_rtx_SET (step, gen_rtx_ASHIFT (Pmode, cnt, GEN_INT (shift)))); |
| 1378 |
emit_insn (gen_rtx_SET (src_addr, gen_rtx_PLUS (Pmode, src_addr, step))); |
| 1379 |
|
| 1380 |
/* Emit a first-fault load. */ |
| 1381 |
rtx vlops[] = {vec, vsrc}; |
| 1382 |
emit_vlmax_insn (code_for_pred_fault_load (vmode), |
| 1383 |
riscv_vector::UNARY_OP, vlops); |
| 1384 |
|
| 1385 |
/* Read how far we read. */ |
| 1386 |
emit_insn (gen_read_vl (Pmode, cnt)); |
| 1387 |
|
| 1388 |
/* Compare needle with haystack and store in a mask. */ |
| 1389 |
rtx eq = gen_rtx_EQ (mask_mode, gen_const_vec_duplicate (vmode, needle), vec); |
| 1390 |
rtx vmsops[] = {mask, eq, vec, needle}; |
| 1391 |
emit_nonvlmax_insn (code_for_pred_cmp_scalar (vmode), |
| 1392 |
riscv_vector::COMPARE_OP, vmsops, cnt); |
| 1393 |
|
| 1394 |
/* Find the first bit in the mask. */ |
| 1395 |
rtx vfops[] = {end, mask}; |
| 1396 |
emit_nonvlmax_insn (code_for_pred_ffs (mask_mode, Pmode), |
| 1397 |
riscv_vector::CPOP_OP, vfops, cnt); |
| 1398 |
|
| 1399 |
/* Emit the loop condition. */ |
| 1400 |
rtx test = gen_rtx_LT (VOIDmode, end, const0_rtx); |
| 1401 |
emit_jump_insn (gen_cbranch4 (Pmode, test, end, const0_rtx, loop)); |
| 1402 |
|
| 1403 |
if (strlen) |
| 1404 |
{ |
| 1405 |
/* For strlen, return the length. */ |
| 1406 |
emit_insn (gen_rtx_SET (dst, gen_rtx_PLUS (Pmode, src_addr, end))); |
| 1407 |
emit_insn (gen_rtx_SET (dst, gen_rtx_MINUS (Pmode, dst, start_addr))); |
| 1408 |
} |
| 1409 |
else |
| 1410 |
{ |
| 1411 |
/* For rawmemchr, return the position at SRC + END * [1,2,4,8]. */ |
| 1412 |
emit_insn (gen_rtx_SET (end, gen_rtx_ASHIFT (Pmode, end, GEN_INT (shift)))); |
| 1413 |
emit_insn (gen_rtx_SET (dst, gen_rtx_PLUS (Pmode, src_addr, end))); |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
/* Implement cmpstr<mode> using vector instructions. The ALIGNMENT and |
| 1418 |
NCOMPARE parameters are unused for now. */ |
| 1419 |
|
| 1420 |
bool |
| 1421 |
expand_strcmp (rtx result, rtx src1, rtx src2, rtx nbytes, |
| 1422 |
unsigned HOST_WIDE_INT, bool) |
| 1423 |
{ |
| 1424 |
gcc_assert (TARGET_VECTOR); |
| 1425 |
|
| 1426 |
/* We don't support big endian. */ |
| 1427 |
if (BYTES_BIG_ENDIAN) |
| 1428 |
return false; |
| 1429 |
|
| 1430 |
bool with_length = nbytes != NULL_RTX; |
| 1431 |
|
| 1432 |
if (with_length |
| 1433 |
&& (!REG_P (nbytes) && !SUBREG_P (nbytes) && !CONST_INT_P (nbytes))) |
| 1434 |
return false; |
| 1435 |
|
| 1436 |
if (with_length && CONST_INT_P (nbytes)) |
| 1437 |
nbytes = force_reg (Pmode, nbytes); |
| 1438 |
|
| 1439 |
machine_mode mode = E_QImode; |
| 1440 |
unsigned int isize = GET_MODE_SIZE (mode).to_constant (); |
| 1441 |
int lmul = TARGET_MAX_LMUL; |
| 1442 |
poly_int64 nunits = exact_div (BYTES_PER_RISCV_VECTOR * lmul, isize); |
| 1443 |
|
| 1444 |
machine_mode vmode; |
| 1445 |
if (!riscv_vector::get_vector_mode (GET_MODE_INNER (mode), nunits) |
| 1446 |
.exists (&vmode)) |
| 1447 |
gcc_unreachable (); |
| 1448 |
|
| 1449 |
machine_mode mask_mode = riscv_vector::get_mask_mode (vmode); |
| 1450 |
|
| 1451 |
/* Prepare addresses. */ |
| 1452 |
rtx src_addr1 = copy_addr_to_reg (XEXP (src1, 0)); |
| 1453 |
rtx vsrc1 = change_address (src1, vmode, src_addr1); |
| 1454 |
|
| 1455 |
rtx src_addr2 = copy_addr_to_reg (XEXP (src2, 0)); |
| 1456 |
rtx vsrc2 = change_address (src2, vmode, src_addr2); |
| 1457 |
|
| 1458 |
/* Set initial pointer bump to 0. */ |
| 1459 |
rtx cnt = gen_reg_rtx (Pmode); |
| 1460 |
emit_move_insn (cnt, CONST0_RTX (Pmode)); |
| 1461 |
|
| 1462 |
rtx sub = gen_reg_rtx (Pmode); |
| 1463 |
emit_move_insn (sub, CONST0_RTX (Pmode)); |
| 1464 |
|
| 1465 |
/* Create source vectors. */ |
| 1466 |
rtx vec1 = gen_reg_rtx (vmode); |
| 1467 |
rtx vec2 = gen_reg_rtx (vmode); |
| 1468 |
|
| 1469 |
rtx done = gen_label_rtx (); |
| 1470 |
rtx loop = gen_label_rtx (); |
| 1471 |
emit_label (loop); |
| 1472 |
|
| 1473 |
/* Bump the pointers. */ |
| 1474 |
emit_insn (gen_rtx_SET (src_addr1, gen_rtx_PLUS (Pmode, src_addr1, cnt))); |
| 1475 |
emit_insn (gen_rtx_SET (src_addr2, gen_rtx_PLUS (Pmode, src_addr2, cnt))); |
| 1476 |
|
| 1477 |
rtx vlops1[] = {vec1, vsrc1}; |
| 1478 |
rtx vlops2[] = {vec2, vsrc2}; |
| 1479 |
|
| 1480 |
if (!with_length) |
| 1481 |
{ |
| 1482 |
emit_vlmax_insn (code_for_pred_fault_load (vmode), |
| 1483 |
riscv_vector::UNARY_OP, vlops1); |
| 1484 |
|
| 1485 |
emit_vlmax_insn (code_for_pred_fault_load (vmode), |
| 1486 |
riscv_vector::UNARY_OP, vlops2); |
| 1487 |
} |
| 1488 |
else |
| 1489 |
{ |
| 1490 |
nbytes = gen_lowpart (Pmode, nbytes); |
| 1491 |
emit_nonvlmax_insn (code_for_pred_fault_load (vmode), |
| 1492 |
riscv_vector::UNARY_OP, vlops1, nbytes); |
| 1493 |
|
| 1494 |
emit_nonvlmax_insn (code_for_pred_fault_load (vmode), |
| 1495 |
riscv_vector::UNARY_OP, vlops2, nbytes); |
| 1496 |
} |
| 1497 |
|
| 1498 |
/* Read the vl for the next pointer bump. */ |
| 1499 |
emit_insn (gen_read_vl (Pmode, cnt)); |
| 1500 |
|
| 1501 |
if (with_length) |
| 1502 |
{ |
| 1503 |
rtx test_done = gen_rtx_EQ (VOIDmode, cnt, const0_rtx); |
| 1504 |
emit_jump_insn (gen_cbranch4 (Pmode, test_done, cnt, const0_rtx, done)); |
| 1505 |
emit_insn (gen_rtx_SET (nbytes, gen_rtx_MINUS (Pmode, nbytes, cnt))); |
| 1506 |
} |
| 1507 |
|
| 1508 |
/* Look for a \0 in the first string. */ |
| 1509 |
rtx mask0 = gen_reg_rtx (mask_mode); |
| 1510 |
rtx eq0 |
| 1511 |
= gen_rtx_EQ (mask_mode, gen_const_vec_duplicate (vmode, CONST0_RTX (mode)), |
| 1512 |
vec1); |
| 1513 |
rtx vmsops1[] = {mask0, eq0, vec1, CONST0_RTX (mode)}; |
| 1514 |
emit_nonvlmax_insn (code_for_pred_cmp_scalar (vmode), |
| 1515 |
riscv_vector::COMPARE_OP, vmsops1, cnt); |
| 1516 |
|
| 1517 |
/* Look for vec1 != vec2 (includes vec2[i] == 0). */ |
| 1518 |
rtx maskne = gen_reg_rtx (mask_mode); |
| 1519 |
rtx ne = gen_rtx_NE (mask_mode, vec1, vec2); |
| 1520 |
rtx vmsops[] = {maskne, ne, vec1, vec2}; |
| 1521 |
emit_nonvlmax_insn (code_for_pred_cmp (vmode), riscv_vector::COMPARE_OP, |
| 1522 |
vmsops, cnt); |
| 1523 |
|
| 1524 |
/* Combine both masks into one. */ |
| 1525 |
rtx mask = gen_reg_rtx (mask_mode); |
| 1526 |
rtx vmorops[] = {mask, mask0, maskne}; |
| 1527 |
emit_nonvlmax_insn (code_for_pred (IOR, mask_mode), |
| 1528 |
riscv_vector::BINARY_MASK_OP, vmorops, cnt); |
| 1529 |
|
| 1530 |
/* Find the first bit in the mask (the first unequal element). */ |
| 1531 |
rtx found_at = gen_reg_rtx (Pmode); |
| 1532 |
rtx vfops[] = {found_at, mask}; |
| 1533 |
emit_nonvlmax_insn (code_for_pred_ffs (mask_mode, Pmode), |
| 1534 |
riscv_vector::CPOP_OP, vfops, cnt); |
| 1535 |
|
| 1536 |
/* Emit the loop condition. */ |
| 1537 |
rtx test = gen_rtx_LT (VOIDmode, found_at, const0_rtx); |
| 1538 |
emit_jump_insn (gen_cbranch4 (Pmode, test, found_at, const0_rtx, loop)); |
| 1539 |
|
| 1540 |
/* Walk up to the difference point. */ |
| 1541 |
emit_insn ( |
| 1542 |
gen_rtx_SET (src_addr1, gen_rtx_PLUS (Pmode, src_addr1, found_at))); |
| 1543 |
emit_insn ( |
| 1544 |
gen_rtx_SET (src_addr2, gen_rtx_PLUS (Pmode, src_addr2, found_at))); |
| 1545 |
|
| 1546 |
/* Load the respective byte and compute the difference. */ |
| 1547 |
rtx c1 = gen_reg_rtx (Pmode); |
| 1548 |
rtx c2 = gen_reg_rtx (Pmode); |
| 1549 |
|
| 1550 |
do_load_from_addr (mode, c1, src_addr1, src1); |
| 1551 |
do_load_from_addr (mode, c2, src_addr2, src2); |
| 1552 |
|
| 1553 |
do_sub3 (sub, c1, c2); |
| 1554 |
|
| 1555 |
if (with_length) |
| 1556 |
emit_label (done); |
| 1557 |
|
| 1558 |
emit_move_insn (result, sub); |
| 1559 |
return true; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Check we are permitted to vectorise a memory operation. |
| 1563 |
If so, return true and populate lmul_out. |
| 1564 |
Otherwise, return false and leave lmul_out unchanged. */ |
| 1565 |
static bool |
| 1566 |
check_vectorise_memory_operation (rtx length_in, HOST_WIDE_INT &lmul_out) |
| 1567 |
{ |
| 1568 |
/* If we either can't or have been asked not to vectorise, respect this. */ |
| 1569 |
if (!TARGET_VECTOR) |
| 1570 |
return false; |
| 1571 |
if (!(stringop_strategy & STRATEGY_VECTOR)) |
| 1572 |
return false; |
| 1573 |
|
| 1574 |
/* If we can't reason about the length, don't vectorise. */ |
| 1575 |
if (!CONST_INT_P (length_in)) |
| 1576 |
return false; |
| 1577 |
|
| 1578 |
HOST_WIDE_INT length = INTVAL (length_in); |
| 1579 |
|
| 1580 |
if (TARGET_XTHEADVECTOR |
| 1581 |
&& known_lt (length, BYTES_PER_RISCV_VECTOR)) |
| 1582 |
return false; |
| 1583 |
|
| 1584 |
/* If it's tiny, default operation is likely better; maybe worth |
| 1585 |
considering fractional lmul in the future as well. */ |
| 1586 |
if (length < (TARGET_MIN_VLEN / 8)) |
| 1587 |
return false; |
| 1588 |
|
| 1589 |
/* If we've been asked to use a specific LMUL, |
| 1590 |
check the operation fits and do that. */ |
| 1591 |
if (rvv_max_lmul != RVV_DYNAMIC) |
| 1592 |
{ |
| 1593 |
lmul_out = TARGET_MAX_LMUL; |
| 1594 |
return (length <= ((TARGET_MAX_LMUL * TARGET_MIN_VLEN) / 8)); |
| 1595 |
} |
| 1596 |
|
| 1597 |
/* Find smallest lmul large enough for entire op. */ |
| 1598 |
HOST_WIDE_INT lmul = 1; |
| 1599 |
while ((lmul <= 8) && (length > ((lmul * TARGET_MIN_VLEN) / 8))) |
| 1600 |
{ |
| 1601 |
lmul <<= 1; |
| 1602 |
} |
| 1603 |
|
| 1604 |
if (lmul > 8) |
| 1605 |
return false; |
| 1606 |
|
| 1607 |
lmul_out = lmul; |
| 1608 |
return true; |
| 1609 |
} |
| 1610 |
|
| 1611 |
/* Used by setmemdi in riscv.md. */ |
| 1612 |
bool |
| 1613 |
expand_vec_setmem (rtx dst_in, rtx length_in, rtx fill_value_in) |
| 1614 |
{ |
| 1615 |
stringop_info info; |
| 1616 |
|
| 1617 |
/* Check we are able and allowed to vectorise this operation; |
| 1618 |
bail if not. */ |
| 1619 |
if (!use_vector_stringop_p (info, 1, length_in) || info.need_loop) |
| 1620 |
return false; |
| 1621 |
|
| 1622 |
rtx dst_addr = copy_addr_to_reg (XEXP (dst_in, 0)); |
| 1623 |
rtx dst = change_address (dst_in, info.vmode, dst_addr); |
| 1624 |
|
| 1625 |
rtx fill_value = gen_reg_rtx (info.vmode); |
| 1626 |
rtx broadcast_ops[] = { fill_value, fill_value_in }; |
| 1627 |
|
| 1628 |
/* If the length is exactly vlmax for the selected mode, do that. |
| 1629 |
Otherwise, use a predicated store. */ |
| 1630 |
if (known_eq (GET_MODE_SIZE (info.vmode), INTVAL (info.avl))) |
| 1631 |
{ |
| 1632 |
emit_vlmax_insn (code_for_pred_broadcast (info.vmode), UNARY_OP, |
| 1633 |
broadcast_ops); |
| 1634 |
emit_move_insn (dst, fill_value); |
| 1635 |
} |
| 1636 |
else |
| 1637 |
{ |
| 1638 |
if (!satisfies_constraint_vl (info.avl)) |
| 1639 |
info.avl = force_reg (Pmode, info.avl); |
| 1640 |
emit_nonvlmax_insn (code_for_pred_broadcast (info.vmode), |
| 1641 |
riscv_vector::UNARY_OP, broadcast_ops, info.avl); |
| 1642 |
machine_mode mask_mode |
| 1643 |
= riscv_vector::get_vector_mode (BImode, GET_MODE_NUNITS (info.vmode)) |
| 1644 |
.require (); |
| 1645 |
rtx mask = CONSTM1_RTX (mask_mode); |
| 1646 |
emit_insn (gen_pred_store (info.vmode, dst, mask, fill_value, info.avl, |
| 1647 |
get_avl_type_rtx (riscv_vector::NONVLMAX))); |
| 1648 |
} |
| 1649 |
|
| 1650 |
return true; |
| 1651 |
} |
| 1652 |
|
| 1653 |
/* Used by cmpmemsi in riscv.md. */ |
| 1654 |
|
| 1655 |
bool |
| 1656 |
expand_vec_cmpmem (rtx result_out, rtx blk_a_in, rtx blk_b_in, rtx length_in) |
| 1657 |
{ |
| 1658 |
HOST_WIDE_INT lmul; |
| 1659 |
/* Check we are able and allowed to vectorise this operation; |
| 1660 |
bail if not. */ |
| 1661 |
if (!check_vectorise_memory_operation (length_in, lmul)) |
| 1662 |
return false; |
| 1663 |
|
| 1664 |
/* Strategy: |
| 1665 |
load entire blocks at a and b into vector regs |
| 1666 |
generate mask of bytes that differ |
| 1667 |
find first set bit in mask |
| 1668 |
find offset of first set bit in mask, use 0 if none set |
| 1669 |
result is ((char*)a[offset] - (char*)b[offset]) |
| 1670 |
*/ |
| 1671 |
|
| 1672 |
machine_mode vmode |
| 1673 |
= riscv_vector::get_vector_mode (QImode, BYTES_PER_RISCV_VECTOR * lmul) |
| 1674 |
.require (); |
| 1675 |
rtx blk_a_addr = copy_addr_to_reg (XEXP (blk_a_in, 0)); |
| 1676 |
rtx blk_a = change_address (blk_a_in, vmode, blk_a_addr); |
| 1677 |
rtx blk_b_addr = copy_addr_to_reg (XEXP (blk_b_in, 0)); |
| 1678 |
rtx blk_b = change_address (blk_b_in, vmode, blk_b_addr); |
| 1679 |
|
| 1680 |
rtx vec_a = gen_reg_rtx (vmode); |
| 1681 |
rtx vec_b = gen_reg_rtx (vmode); |
| 1682 |
|
| 1683 |
machine_mode mask_mode = get_mask_mode (vmode); |
| 1684 |
rtx mask = gen_reg_rtx (mask_mode); |
| 1685 |
rtx mismatch_ofs = gen_reg_rtx (Pmode); |
| 1686 |
|
| 1687 |
rtx ne = gen_rtx_NE (mask_mode, vec_a, vec_b); |
| 1688 |
rtx vmsops[] = { mask, ne, vec_a, vec_b }; |
| 1689 |
rtx vfops[] = { mismatch_ofs, mask }; |
| 1690 |
|
| 1691 |
/* If the length is exactly vlmax for the selected mode, do that. |
| 1692 |
Otherwise, use a predicated store. */ |
| 1693 |
|
| 1694 |
if (known_eq (GET_MODE_SIZE (vmode), INTVAL (length_in))) |
| 1695 |
{ |
| 1696 |
emit_move_insn (vec_a, blk_a); |
| 1697 |
emit_move_insn (vec_b, blk_b); |
| 1698 |
emit_vlmax_insn (code_for_pred_cmp (vmode), riscv_vector::COMPARE_OP, |
| 1699 |
vmsops); |
| 1700 |
|
| 1701 |
emit_vlmax_insn (code_for_pred_ffs (mask_mode, Pmode), |
| 1702 |
riscv_vector::CPOP_OP, vfops); |
| 1703 |
} |
| 1704 |
else |
| 1705 |
{ |
| 1706 |
if (!satisfies_constraint_vl (length_in)) |
| 1707 |
length_in = force_reg (Pmode, length_in); |
| 1708 |
|
| 1709 |
rtx memmask = CONSTM1_RTX (mask_mode); |
| 1710 |
|
| 1711 |
rtx m_ops_a[] = { vec_a, memmask, blk_a }; |
| 1712 |
rtx m_ops_b[] = { vec_b, memmask, blk_b }; |
| 1713 |
|
| 1714 |
emit_nonvlmax_insn (code_for_pred_mov (vmode), |
| 1715 |
riscv_vector::UNARY_OP_TAMA, m_ops_a, length_in); |
| 1716 |
emit_nonvlmax_insn (code_for_pred_mov (vmode), |
| 1717 |
riscv_vector::UNARY_OP_TAMA, m_ops_b, length_in); |
| 1718 |
|
| 1719 |
emit_nonvlmax_insn (code_for_pred_cmp (vmode), riscv_vector::COMPARE_OP, |
| 1720 |
vmsops, length_in); |
| 1721 |
|
| 1722 |
emit_nonvlmax_insn (code_for_pred_ffs (mask_mode, Pmode), |
| 1723 |
riscv_vector::CPOP_OP, vfops, length_in); |
| 1724 |
} |
| 1725 |
|
| 1726 |
/* Mismatch_ofs is -1 if blocks match, or the offset of |
| 1727 |
the first mismatch otherwise. */ |
| 1728 |
rtx ltz = gen_reg_rtx (Xmode); |
| 1729 |
emit_insn (gen_slt_3 (LT, Xmode, Xmode, ltz, mismatch_ofs, const0_rtx)); |
| 1730 |
/* mismatch_ofs += (mismatch_ofs < 0) ? 1 : 0. */ |
| 1731 |
emit_insn ( |
| 1732 |
gen_rtx_SET (mismatch_ofs, gen_rtx_PLUS (Pmode, mismatch_ofs, ltz))); |
| 1733 |
|
| 1734 |
/* Unconditionally load the bytes at mismatch_ofs and subtract them |
| 1735 |
to get our result. */ |
| 1736 |
emit_insn (gen_rtx_SET (blk_a_addr, |
| 1737 |
gen_rtx_PLUS (Pmode, mismatch_ofs, blk_a_addr))); |
| 1738 |
emit_insn (gen_rtx_SET (blk_b_addr, |
| 1739 |
gen_rtx_PLUS (Pmode, mismatch_ofs, blk_b_addr))); |
| 1740 |
|
| 1741 |
blk_a = change_address (blk_a, QImode, blk_a_addr); |
| 1742 |
blk_b = change_address (blk_b, QImode, blk_b_addr); |
| 1743 |
|
| 1744 |
rtx byte_a = gen_reg_rtx (SImode); |
| 1745 |
rtx byte_b = gen_reg_rtx (SImode); |
| 1746 |
do_zero_extendqi2 (byte_a, blk_a); |
| 1747 |
do_zero_extendqi2 (byte_b, blk_b); |
| 1748 |
|
| 1749 |
emit_insn (gen_rtx_SET (result_out, gen_rtx_MINUS (SImode, byte_a, byte_b))); |
| 1750 |
|
| 1751 |
return true; |
| 1752 |
} |
| 1753 |
} |