| 1 |
/* Cost model implementation for RISC-V 'V' Extension for GNU compiler. |
| 2 |
Copyright (C) 2023-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Juzhe Zhong ([email protected]), RiVAI Technologies Ltd. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#define IN_TARGET_CODE 1 |
| 22 |
|
| 23 |
#define INCLUDE_STRING |
| 24 |
#include "config.h" |
| 25 |
#include "system.h" |
| 26 |
#include "coretypes.h" |
| 27 |
#include "tm.h" |
| 28 |
#include "target.h" |
| 29 |
#include "function.h" |
| 30 |
#include "tree.h" |
| 31 |
#include "basic-block.h" |
| 32 |
#include "rtl.h" |
| 33 |
#include "gimple.h" |
| 34 |
#include "targhooks.h" |
| 35 |
#include "cfgloop.h" |
| 36 |
#include "fold-const.h" |
| 37 |
#include "tm_p.h" |
| 38 |
#include "tree-vectorizer.h" |
| 39 |
#include "gimple-iterator.h" |
| 40 |
#include "bitmap.h" |
| 41 |
#include "ssa.h" |
| 42 |
#include "backend.h" |
| 43 |
#include "tree-data-ref.h" |
| 44 |
#include "tree-ssa-loop-niter.h" |
| 45 |
#include "tree-hash-traits.h" |
| 46 |
|
| 47 |
/* This file should be included last. */ |
| 48 |
#include "riscv-vector-costs.h" |
| 49 |
|
| 50 |
namespace riscv_vector { |
| 51 |
|
| 52 |
/* Dynamic LMUL philosophy - Local linear-scan SSA live range based analysis |
| 53 |
determine LMUL |
| 54 |
|
| 55 |
- Collect all vectorize STMTs locally for each loop block. |
| 56 |
- Build program point based graph, ignore non-vectorize STMTs: |
| 57 |
|
| 58 |
vectorize STMT 0 - point 0 |
| 59 |
scalar STMT 0 - ignore. |
| 60 |
vectorize STMT 1 - point 1 |
| 61 |
... |
| 62 |
- Compute the number of live V_REGs live at each program point |
| 63 |
- Determine LMUL in VECTOR COST model according to the program point |
| 64 |
which has maximum live V_REGs. |
| 65 |
|
| 66 |
Note: |
| 67 |
|
| 68 |
- BIGGEST_MODE is the biggest LMUL auto-vectorization element mode. |
| 69 |
It's important for mixed size auto-vectorization (Conversions, ... etc). |
| 70 |
E.g. For a loop that is vectorizing conversion of INT32 -> INT64. |
| 71 |
The biggest mode is DImode and LMUL = 8, LMUL = 4 for SImode. |
| 72 |
We compute the number live V_REGs at each program point according to |
| 73 |
this information. |
| 74 |
- We only compute program points and live ranges locally (within a block) |
| 75 |
since we just need to compute the number of live V_REGs at each program |
| 76 |
point and we are not really allocating the registers for each SSA. |
| 77 |
We can make the variable has another local live range in another block |
| 78 |
if it live out/live in to another block. Such approach doesn't affect |
| 79 |
out accurate live range analysis. |
| 80 |
- Current analysis didn't consider any instruction scheduling which |
| 81 |
may improve the register pressure. So we are conservatively doing the |
| 82 |
analysis which may end up with smaller LMUL. |
| 83 |
TODO: Maybe we could support a reasonable live range shrink algorithm |
| 84 |
which take advantage of instruction scheduling. |
| 85 |
- We may have these following possible autovec modes analysis: |
| 86 |
|
| 87 |
1. M8 -> M4 -> M2 -> M1 (stop analysis here) -> MF2 -> MF4 -> MF8 |
| 88 |
2. M8 -> M1(M4) -> MF2(M2) -> MF4(M1) (stop analysis here) -> MF8(MF2) |
| 89 |
3. M1(M8) -> MF2(M4) -> MF4(M2) -> MF8(M1) |
| 90 |
*/ |
| 91 |
|
| 92 |
static bool |
| 93 |
is_gimple_assign_or_call (gimple *stmt) |
| 94 |
{ |
| 95 |
return is_gimple_assign (stmt) || is_gimple_call (stmt); |
| 96 |
} |
| 97 |
|
| 98 |
/* Return the program point of 1st vectorized lanes statement. */ |
| 99 |
static unsigned int |
| 100 |
get_first_lane_point (const vec<stmt_point> program_points, |
| 101 |
stmt_vec_info stmt_info) |
| 102 |
{ |
| 103 |
for (const auto program_point : program_points) |
| 104 |
if (program_point.stmt_info == DR_GROUP_FIRST_ELEMENT (stmt_info)) |
| 105 |
return program_point.point; |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
/* Return the program point of last vectorized lanes statement. */ |
| 110 |
static unsigned int |
| 111 |
get_last_lane_point (const vec<stmt_point> program_points, |
| 112 |
stmt_vec_info stmt_info) |
| 113 |
{ |
| 114 |
unsigned int max_point = 0; |
| 115 |
for (auto s = DR_GROUP_FIRST_ELEMENT (stmt_info); s != NULL; |
| 116 |
s = DR_GROUP_NEXT_ELEMENT (s)) |
| 117 |
{ |
| 118 |
for (const auto program_point : program_points) |
| 119 |
if (program_point.stmt_info == s && program_point.point > max_point) |
| 120 |
max_point = program_point.point; |
| 121 |
} |
| 122 |
return max_point; |
| 123 |
} |
| 124 |
|
| 125 |
/* Return the last variable that is in the live range list. */ |
| 126 |
static pair * |
| 127 |
get_live_range (hash_map<tree, pair> *live_ranges, tree arg) |
| 128 |
{ |
| 129 |
auto *r = live_ranges->get (arg); |
| 130 |
if (r) |
| 131 |
return r; |
| 132 |
else |
| 133 |
{ |
| 134 |
tree t = arg; |
| 135 |
gimple *def_stmt = NULL; |
| 136 |
while (t && TREE_CODE (t) == SSA_NAME && !r |
| 137 |
&& (def_stmt = SSA_NAME_DEF_STMT (t))) |
| 138 |
{ |
| 139 |
if (gimple_assign_cast_p (def_stmt)) |
| 140 |
{ |
| 141 |
t = gimple_assign_rhs1 (def_stmt); |
| 142 |
r = live_ranges->get (t); |
| 143 |
def_stmt = NULL; |
| 144 |
} |
| 145 |
else |
| 146 |
/* FIXME: Currently we don't see any fold for |
| 147 |
non-conversion statements. */ |
| 148 |
t = NULL_TREE; |
| 149 |
} |
| 150 |
if (r) |
| 151 |
return r; |
| 152 |
else |
| 153 |
{ |
| 154 |
bool insert_p = live_ranges->put (arg, pair (0, 0)); |
| 155 |
gcc_assert (!insert_p); |
| 156 |
return live_ranges->get (arg); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/* Collect all STMTs that are vectorized and compute their program points. |
| 162 |
Note that we don't care about the STMTs that are not vectorized and |
| 163 |
we only build the local graph (within a block) of program points. |
| 164 |
|
| 165 |
Loop: |
| 166 |
bb 2: |
| 167 |
STMT 1 (be vectorized) -- point 0 |
| 168 |
STMT 2 (not be vectorized) -- ignored |
| 169 |
STMT 3 (be vectorized) -- point 1 |
| 170 |
STMT 4 (be vectorized) -- point 2 |
| 171 |
STMT 5 (be vectorized) -- point 3 |
| 172 |
... |
| 173 |
bb 3: |
| 174 |
STMT 1 (be vectorized) -- point 0 |
| 175 |
STMT 2 (be vectorized) -- point 1 |
| 176 |
STMT 3 (not be vectorized) -- ignored |
| 177 |
STMT 4 (not be vectorized) -- ignored |
| 178 |
STMT 5 (be vectorized) -- point 2 |
| 179 |
... |
| 180 |
*/ |
| 181 |
static void |
| 182 |
compute_local_program_points ( |
| 183 |
vec_info *vinfo, |
| 184 |
hash_map<basic_block, vec<stmt_point>> &program_points_per_bb) |
| 185 |
{ |
| 186 |
if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo)) |
| 187 |
{ |
| 188 |
class loop *loop = LOOP_VINFO_LOOP (loop_vinfo); |
| 189 |
basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo); |
| 190 |
unsigned int nbbs = loop->num_nodes; |
| 191 |
gimple_stmt_iterator si; |
| 192 |
unsigned int i; |
| 193 |
/* Collect the stmts that is vectorized and mark their program point. */ |
| 194 |
for (i = 0; i < nbbs; i++) |
| 195 |
{ |
| 196 |
unsigned int point = 1; |
| 197 |
basic_block bb = bbs[i]; |
| 198 |
vec<stmt_point> program_points = vNULL; |
| 199 |
if (dump_enabled_p ()) |
| 200 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 201 |
"Compute local program points for bb %d:\n", |
| 202 |
bb->index); |
| 203 |
for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) |
| 204 |
{ |
| 205 |
if (!is_gimple_assign_or_call (gsi_stmt (si))) |
| 206 |
continue; |
| 207 |
stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (si)); |
| 208 |
enum stmt_vec_info_type type |
| 209 |
= STMT_VINFO_TYPE (vect_stmt_to_vectorize (stmt_info)); |
| 210 |
if (type != undef_vec_info_type) |
| 211 |
{ |
| 212 |
stmt_point info = {point, gsi_stmt (si), stmt_info}; |
| 213 |
program_points.safe_push (info); |
| 214 |
point++; |
| 215 |
if (dump_enabled_p ()) |
| 216 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 217 |
"program point %d: %G", info.point, |
| 218 |
gsi_stmt (si)); |
| 219 |
} |
| 220 |
|
| 221 |
/* If the statement is part of a pattern, also add the other |
| 222 |
pattern statements. */ |
| 223 |
gimple_seq pattern_def_seq; |
| 224 |
if (STMT_VINFO_IN_PATTERN_P (stmt_info) |
| 225 |
&& (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info))) |
| 226 |
{ |
| 227 |
gimple_stmt_iterator si2; |
| 228 |
|
| 229 |
for (si2 = gsi_start (pattern_def_seq); |
| 230 |
!gsi_end_p (si2); |
| 231 |
gsi_next (&si2)) |
| 232 |
{ |
| 233 |
stmt_vec_info pattern_def_stmt_info |
| 234 |
= vinfo->lookup_stmt (gsi_stmt (si2)); |
| 235 |
if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info) |
| 236 |
|| STMT_VINFO_LIVE_P (pattern_def_stmt_info)) |
| 237 |
{ |
| 238 |
stmt_point info = {point, gsi_stmt (si2), |
| 239 |
pattern_def_stmt_info}; |
| 240 |
program_points.safe_push (info); |
| 241 |
point++; |
| 242 |
if (dump_enabled_p ()) |
| 243 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 244 |
"program point %d: %G", |
| 245 |
info.point, gsi_stmt (si2)); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
program_points_per_bb.put (bb, program_points); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
static machine_mode |
| 256 |
get_biggest_mode (machine_mode mode1, machine_mode mode2) |
| 257 |
{ |
| 258 |
unsigned int mode1_size = GET_MODE_BITSIZE (mode1).to_constant (); |
| 259 |
unsigned int mode2_size = GET_MODE_BITSIZE (mode2).to_constant (); |
| 260 |
return mode1_size >= mode2_size ? mode1 : mode2; |
| 261 |
} |
| 262 |
|
| 263 |
/* Return true if OP is invariant. */ |
| 264 |
|
| 265 |
static bool |
| 266 |
loop_invariant_op_p (class loop *loop, |
| 267 |
tree op) |
| 268 |
{ |
| 269 |
if (is_gimple_constant (op)) |
| 270 |
return true; |
| 271 |
if (SSA_NAME_IS_DEFAULT_DEF (op) |
| 272 |
|| !flow_bb_inside_loop_p (loop, gimple_bb (SSA_NAME_DEF_STMT (op)))) |
| 273 |
return true; |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
/* Return true if the variable should be counted into liveness. */ |
| 278 |
static bool |
| 279 |
variable_vectorized_p (class loop *loop, stmt_vec_info stmt_info, tree var, |
| 280 |
bool lhs_p) |
| 281 |
{ |
| 282 |
if (!var) |
| 283 |
return false; |
| 284 |
gimple *stmt = STMT_VINFO_STMT (stmt_info); |
| 285 |
enum stmt_vec_info_type type |
| 286 |
= STMT_VINFO_TYPE (vect_stmt_to_vectorize (stmt_info)); |
| 287 |
if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) |
| 288 |
{ |
| 289 |
if (gimple_call_internal_fn (stmt) == IFN_MASK_STORE |
| 290 |
|| gimple_call_internal_fn (stmt) == IFN_MASK_LOAD) |
| 291 |
{ |
| 292 |
/* .MASK_LOAD (_5, 32B, _33) |
| 293 |
^ ^ ^ |
| 294 |
Only the 3rd argument will be vectorized and consume |
| 295 |
a vector register. */ |
| 296 |
if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE |
| 297 |
|| (is_gimple_reg (var) && !POINTER_TYPE_P (TREE_TYPE (var)))) |
| 298 |
return true; |
| 299 |
else |
| 300 |
return false; |
| 301 |
} |
| 302 |
} |
| 303 |
else if (is_gimple_assign (stmt)) |
| 304 |
{ |
| 305 |
tree_code tcode = gimple_assign_rhs_code (stmt); |
| 306 |
/* vi variant doesn't need to allocate such statement. |
| 307 |
E.g. tmp_15 = _4 + 1; will be transformed into vadd.vi |
| 308 |
so the INTEGER_CST '1' doesn't need a vector register. */ |
| 309 |
switch (tcode) |
| 310 |
{ |
| 311 |
case PLUS_EXPR: |
| 312 |
case BIT_IOR_EXPR: |
| 313 |
case BIT_XOR_EXPR: |
| 314 |
case BIT_AND_EXPR: |
| 315 |
return TREE_CODE (var) != INTEGER_CST |
| 316 |
|| !tree_fits_shwi_p (var) |
| 317 |
|| !IN_RANGE (tree_to_shwi (var), -16, 15); |
| 318 |
case MINUS_EXPR: |
| 319 |
return TREE_CODE (var) != INTEGER_CST |
| 320 |
|| !tree_fits_shwi_p (var) |
| 321 |
|| !IN_RANGE (tree_to_shwi (var), -16, 15) |
| 322 |
|| gimple_assign_rhs1 (stmt) != var; |
| 323 |
case LSHIFT_EXPR: |
| 324 |
case RSHIFT_EXPR: |
| 325 |
return gimple_assign_rhs2 (stmt) != var |
| 326 |
|| !loop_invariant_op_p (loop, var); |
| 327 |
default: |
| 328 |
break; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if (lhs_p) |
| 333 |
return is_gimple_reg (var) |
| 334 |
&& (!POINTER_TYPE_P (TREE_TYPE (var)) |
| 335 |
|| type != store_vec_info_type); |
| 336 |
else |
| 337 |
return poly_int_tree_p (var) |
| 338 |
|| (is_gimple_val (var) |
| 339 |
&& (!POINTER_TYPE_P (TREE_TYPE (var)) |
| 340 |
|| type != load_vec_info_type)); |
| 341 |
} |
| 342 |
|
| 343 |
/* Compute local live ranges of each vectorized variable. |
| 344 |
Note that we only compute local live ranges (within a block) since |
| 345 |
local live ranges information is accurate enough for us to determine |
| 346 |
the LMUL/vectorization factor of the loop. |
| 347 |
|
| 348 |
Loop: |
| 349 |
bb 2: |
| 350 |
STMT 1 -- point 0 |
| 351 |
STMT 2 (def SSA 1) -- point 1 |
| 352 |
STMT 3 (use SSA 1) -- point 2 |
| 353 |
STMT 4 -- point 3 |
| 354 |
bb 3: |
| 355 |
STMT 1 -- point 0 |
| 356 |
STMT 2 -- point 1 |
| 357 |
STMT 3 -- point 2 |
| 358 |
STMT 4 (use SSA 2) -- point 3 |
| 359 |
|
| 360 |
The live range of SSA 1 is [1, 3] in bb 2. |
| 361 |
The live range of SSA 2 is [0, 4] in bb 3. */ |
| 362 |
static machine_mode |
| 363 |
compute_local_live_ranges ( |
| 364 |
loop_vec_info loop_vinfo, |
| 365 |
const hash_map<basic_block, vec<stmt_point>> &program_points_per_bb, |
| 366 |
hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb) |
| 367 |
{ |
| 368 |
machine_mode biggest_mode = QImode; |
| 369 |
class loop *loop = LOOP_VINFO_LOOP (loop_vinfo); |
| 370 |
if (!program_points_per_bb.is_empty ()) |
| 371 |
{ |
| 372 |
auto_vec<tree> visited_vars; |
| 373 |
unsigned int i; |
| 374 |
for (hash_map<basic_block, vec<stmt_point>>::iterator iter |
| 375 |
= program_points_per_bb.begin (); |
| 376 |
iter != program_points_per_bb.end (); ++iter) |
| 377 |
{ |
| 378 |
basic_block bb = (*iter).first; |
| 379 |
vec<stmt_point> program_points = (*iter).second; |
| 380 |
bool existed_p = false; |
| 381 |
hash_map<tree, pair> *live_ranges |
| 382 |
= &live_ranges_per_bb.get_or_insert (bb, &existed_p); |
| 383 |
gcc_assert (!existed_p); |
| 384 |
if (dump_enabled_p ()) |
| 385 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 386 |
"Compute local live ranges for bb %d:\n", |
| 387 |
bb->index); |
| 388 |
for (const auto program_point : program_points) |
| 389 |
{ |
| 390 |
unsigned int point = program_point.point; |
| 391 |
gimple *stmt = program_point.stmt; |
| 392 |
tree lhs = gimple_get_lhs (stmt); |
| 393 |
if (variable_vectorized_p (loop, program_point.stmt_info, lhs, |
| 394 |
true)) |
| 395 |
{ |
| 396 |
biggest_mode = get_biggest_mode (biggest_mode, |
| 397 |
TYPE_MODE (TREE_TYPE (lhs))); |
| 398 |
bool existed_p = false; |
| 399 |
pair &live_range |
| 400 |
= live_ranges->get_or_insert (lhs, &existed_p); |
| 401 |
gcc_assert (!existed_p); |
| 402 |
if (STMT_VINFO_MEMORY_ACCESS_TYPE (program_point.stmt_info) |
| 403 |
== VMAT_LOAD_STORE_LANES) |
| 404 |
point = get_first_lane_point (program_points, |
| 405 |
program_point.stmt_info); |
| 406 |
live_range = pair (point, point); |
| 407 |
} |
| 408 |
for (i = 0; i < gimple_num_args (stmt); i++) |
| 409 |
{ |
| 410 |
tree var = gimple_arg (stmt, i); |
| 411 |
if (variable_vectorized_p (loop, program_point.stmt_info, var, |
| 412 |
false)) |
| 413 |
{ |
| 414 |
biggest_mode |
| 415 |
= get_biggest_mode (biggest_mode, |
| 416 |
TYPE_MODE (TREE_TYPE (var))); |
| 417 |
bool existed_p = false; |
| 418 |
pair &live_range |
| 419 |
= live_ranges->get_or_insert (var, &existed_p); |
| 420 |
if (STMT_VINFO_MEMORY_ACCESS_TYPE ( |
| 421 |
program_point.stmt_info) |
| 422 |
== VMAT_LOAD_STORE_LANES) |
| 423 |
point = get_last_lane_point (program_points, |
| 424 |
program_point.stmt_info); |
| 425 |
else if (existed_p) |
| 426 |
point = MAX (live_range.second, point); |
| 427 |
if (existed_p) |
| 428 |
/* We will grow the live range for each use. */ |
| 429 |
live_range = pair (live_range.first, point); |
| 430 |
else |
| 431 |
{ |
| 432 |
gimple *def_stmt; |
| 433 |
if (TREE_CODE (var) == SSA_NAME |
| 434 |
&& (def_stmt = SSA_NAME_DEF_STMT (var)) |
| 435 |
&& gimple_bb (def_stmt) == bb |
| 436 |
&& is_gimple_assign_or_call (def_stmt)) |
| 437 |
{ |
| 438 |
live_ranges->remove (var); |
| 439 |
for (unsigned int j = 0; |
| 440 |
j < gimple_num_args (def_stmt); j++) |
| 441 |
{ |
| 442 |
tree arg = gimple_arg (def_stmt, j); |
| 443 |
auto *r = get_live_range (live_ranges, arg); |
| 444 |
gcc_assert (r); |
| 445 |
(*r).second = MAX (point, (*r).second); |
| 446 |
biggest_mode = get_biggest_mode ( |
| 447 |
biggest_mode, TYPE_MODE (TREE_TYPE (arg))); |
| 448 |
} |
| 449 |
} |
| 450 |
else |
| 451 |
/* The splat vector lives the whole block. */ |
| 452 |
live_range = pair (0, program_points.length ()); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
if (dump_enabled_p ()) |
| 458 |
for (hash_map<tree, pair>::iterator iter = live_ranges->begin (); |
| 459 |
iter != live_ranges->end (); ++iter) |
| 460 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 461 |
"%T: type = %T, start = %d, end = %d\n", |
| 462 |
(*iter).first, TREE_TYPE ((*iter).first), |
| 463 |
(*iter).second.first, (*iter).second.second); |
| 464 |
} |
| 465 |
} |
| 466 |
if (dump_enabled_p ()) |
| 467 |
dump_printf_loc (MSG_NOTE, vect_location, "Biggest mode = %s\n", |
| 468 |
GET_MODE_NAME (biggest_mode)); |
| 469 |
return biggest_mode; |
| 470 |
} |
| 471 |
|
| 472 |
/* Compute the mode for MODE, BIGGEST_MODE and LMUL. |
| 473 |
|
| 474 |
E.g. If mode = SImode, biggest_mode = DImode, LMUL = M4. |
| 475 |
Then return RVVM4SImode (LMUL = 4, element mode = SImode). */ |
| 476 |
static unsigned int |
| 477 |
compute_nregs_for_mode (loop_vec_info loop_vinfo, machine_mode mode, |
| 478 |
machine_mode biggest_mode, int lmul) |
| 479 |
{ |
| 480 |
unsigned int rgroup_size = LOOP_VINFO_LENS (loop_vinfo).is_empty () |
| 481 |
? 1 |
| 482 |
: LOOP_VINFO_LENS (loop_vinfo).length (); |
| 483 |
unsigned int mode_size = GET_MODE_SIZE (mode).to_constant (); |
| 484 |
unsigned int biggest_size = GET_MODE_SIZE (biggest_mode).to_constant (); |
| 485 |
gcc_assert (biggest_size >= mode_size); |
| 486 |
unsigned int ratio = biggest_size / mode_size; |
| 487 |
/* RVV mask bool modes always consume 1 vector register regardless LMUL. */ |
| 488 |
unsigned int nregs = mode == BImode ? 1 : lmul / ratio; |
| 489 |
return MAX (nregs, 1) * rgroup_size; |
| 490 |
} |
| 491 |
|
| 492 |
/* This function helps to determine whether current LMUL will cause |
| 493 |
potential vector register (V_REG) spillings according to live range |
| 494 |
information. |
| 495 |
|
| 496 |
- First, compute how many variable are alive of each program point |
| 497 |
in each bb of the loop. |
| 498 |
- Second, compute how many V_REGs are alive of each program point |
| 499 |
in each bb of the loop according the BIGGEST_MODE and the variable |
| 500 |
mode. |
| 501 |
- Third, Return the maximum V_REGs are alive of the loop. */ |
| 502 |
static unsigned int |
| 503 |
max_number_of_live_regs (loop_vec_info loop_vinfo, const basic_block bb, |
| 504 |
const hash_map<tree, pair> &live_ranges, |
| 505 |
unsigned int max_point, machine_mode biggest_mode, |
| 506 |
int lmul) |
| 507 |
{ |
| 508 |
unsigned int max_nregs = 0; |
| 509 |
unsigned int i; |
| 510 |
unsigned int live_point = 0; |
| 511 |
auto_vec<unsigned int> live_vars_vec; |
| 512 |
live_vars_vec.safe_grow_cleared (max_point, true); |
| 513 |
for (hash_map<tree, pair>::iterator iter = live_ranges.begin (); |
| 514 |
iter != live_ranges.end (); ++iter) |
| 515 |
{ |
| 516 |
tree var = (*iter).first; |
| 517 |
pair live_range = (*iter).second; |
| 518 |
for (i = live_range.first + 1; i <= live_range.second; i++) |
| 519 |
{ |
| 520 |
machine_mode mode; |
| 521 |
if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE) |
| 522 |
mode = BImode; |
| 523 |
/* Constants do not have a mode, just use the biggest so |
| 524 |
compute_nregs will return 1. */ |
| 525 |
else if (TREE_CODE (var) == INTEGER_CST) |
| 526 |
mode = biggest_mode; |
| 527 |
else |
| 528 |
mode = TYPE_MODE (TREE_TYPE (var)); |
| 529 |
unsigned int nregs |
| 530 |
= compute_nregs_for_mode (loop_vinfo, mode, biggest_mode, lmul); |
| 531 |
live_vars_vec[i] += nregs; |
| 532 |
if (live_vars_vec[i] > max_nregs) |
| 533 |
{ |
| 534 |
max_nregs = live_vars_vec[i]; |
| 535 |
live_point = i; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
/* Collect user explicit RVV type. */ |
| 541 |
auto_vec<basic_block> all_preds |
| 542 |
= get_all_dominated_blocks (CDI_POST_DOMINATORS, bb); |
| 543 |
tree t; |
| 544 |
FOR_EACH_SSA_NAME (i, t, cfun) |
| 545 |
{ |
| 546 |
machine_mode mode = TYPE_MODE (TREE_TYPE (t)); |
| 547 |
if (!lookup_vector_type_attribute (TREE_TYPE (t)) |
| 548 |
&& !riscv_v_ext_vls_mode_p (mode)) |
| 549 |
continue; |
| 550 |
|
| 551 |
gimple *def = SSA_NAME_DEF_STMT (t); |
| 552 |
if (gimple_bb (def) && !all_preds.contains (gimple_bb (def))) |
| 553 |
continue; |
| 554 |
use_operand_p use_p; |
| 555 |
imm_use_iterator iterator; |
| 556 |
|
| 557 |
FOR_EACH_IMM_USE_FAST (use_p, iterator, t) |
| 558 |
{ |
| 559 |
if (!USE_STMT (use_p) || is_gimple_debug (USE_STMT (use_p)) |
| 560 |
|| !dominated_by_p (CDI_POST_DOMINATORS, bb, |
| 561 |
gimple_bb (USE_STMT (use_p)))) |
| 562 |
continue; |
| 563 |
|
| 564 |
int regno_alignment = riscv_get_v_regno_alignment (mode); |
| 565 |
max_nregs += regno_alignment; |
| 566 |
if (dump_enabled_p ()) |
| 567 |
dump_printf_loc ( |
| 568 |
MSG_NOTE, vect_location, |
| 569 |
"Explicit used SSA %T, vectype = %T, mode = %s, cause %d " |
| 570 |
"V_REG live in bb %d at program point %d\n", |
| 571 |
t, TREE_TYPE (t), GET_MODE_NAME (mode), regno_alignment, |
| 572 |
bb->index, live_point); |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
if (dump_enabled_p ()) |
| 578 |
dump_printf_loc ( |
| 579 |
MSG_NOTE, vect_location, |
| 580 |
"Maximum lmul = %d, At most %d number of live V_REG at program " |
| 581 |
"point %d for bb %d\n", |
| 582 |
lmul, max_nregs, live_point, bb->index); |
| 583 |
return max_nregs; |
| 584 |
} |
| 585 |
|
| 586 |
/* Get STORE value. */ |
| 587 |
static tree |
| 588 |
get_store_value (gimple *stmt) |
| 589 |
{ |
| 590 |
if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) |
| 591 |
{ |
| 592 |
if (gimple_call_internal_fn (stmt) == IFN_MASK_STORE) |
| 593 |
return gimple_call_arg (stmt, 3); |
| 594 |
else |
| 595 |
gcc_unreachable (); |
| 596 |
} |
| 597 |
else |
| 598 |
return gimple_assign_rhs1 (stmt); |
| 599 |
} |
| 600 |
|
| 601 |
/* Return true if additional vector vars needed. */ |
| 602 |
static bool |
| 603 |
need_additional_vector_vars_p (stmt_vec_info stmt_info) |
| 604 |
{ |
| 605 |
enum stmt_vec_info_type type |
| 606 |
= STMT_VINFO_TYPE (vect_stmt_to_vectorize (stmt_info)); |
| 607 |
if (type == load_vec_info_type || type == store_vec_info_type) |
| 608 |
{ |
| 609 |
if (STMT_VINFO_GATHER_SCATTER_P (stmt_info) |
| 610 |
&& STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) == VMAT_GATHER_SCATTER) |
| 611 |
return true; |
| 612 |
|
| 613 |
machine_mode mode = TYPE_MODE (STMT_VINFO_VECTYPE (stmt_info)); |
| 614 |
int lmul = riscv_get_v_regno_alignment (mode); |
| 615 |
if (DR_GROUP_SIZE (stmt_info) * lmul > RVV_M8) |
| 616 |
return true; |
| 617 |
} |
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
/* Return the LMUL of the current analysis. */ |
| 622 |
static int |
| 623 |
compute_estimated_lmul (loop_vec_info loop_vinfo, machine_mode mode) |
| 624 |
{ |
| 625 |
gcc_assert (GET_MODE_BITSIZE (mode).is_constant ()); |
| 626 |
int regno_alignment = riscv_get_v_regno_alignment (loop_vinfo->vector_mode); |
| 627 |
if (riscv_v_ext_vls_mode_p (loop_vinfo->vector_mode)) |
| 628 |
return regno_alignment; |
| 629 |
else if (known_eq (LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo), 1U)) |
| 630 |
{ |
| 631 |
int estimated_vf = vect_vf_for_cost (loop_vinfo); |
| 632 |
int estimated_lmul = estimated_vf * GET_MODE_BITSIZE (mode).to_constant () |
| 633 |
/ TARGET_MIN_VLEN; |
| 634 |
if (estimated_lmul > RVV_M8) |
| 635 |
return regno_alignment; |
| 636 |
else |
| 637 |
return estimated_lmul; |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
/* Estimate the VLA SLP LMUL. */ |
| 642 |
if (regno_alignment > RVV_M1) |
| 643 |
return regno_alignment; |
| 644 |
else if (mode != QImode |
| 645 |
|| LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo).is_constant ()) |
| 646 |
{ |
| 647 |
int ratio; |
| 648 |
if (can_div_trunc_p (BYTES_PER_RISCV_VECTOR, |
| 649 |
GET_MODE_SIZE (loop_vinfo->vector_mode), &ratio)) |
| 650 |
{ |
| 651 |
if (ratio == 1) |
| 652 |
return RVV_M4; |
| 653 |
else if (ratio == 2) |
| 654 |
return RVV_M2; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
/* Update the live ranges according PHI. |
| 662 |
|
| 663 |
Loop: |
| 664 |
bb 2: |
| 665 |
STMT 1 -- point 0 |
| 666 |
STMT 2 (def SSA 1) -- point 1 |
| 667 |
STMT 3 (use SSA 1) -- point 2 |
| 668 |
STMT 4 -- point 3 |
| 669 |
bb 3: |
| 670 |
SSA 2 = PHI<SSA 1> |
| 671 |
STMT 1 -- point 0 |
| 672 |
STMT 2 -- point 1 |
| 673 |
STMT 3 (use SSA 2) -- point 2 |
| 674 |
STMT 4 -- point 3 |
| 675 |
|
| 676 |
Before this function, the SSA 1 live range is [2, 3] in bb 2 |
| 677 |
and SSA 2 is [0, 3] in bb 3. |
| 678 |
|
| 679 |
Then, after this function, we update SSA 1 live range in bb 2 |
| 680 |
into [2, 4] since SSA 1 is live out into bb 3. */ |
| 681 |
static void |
| 682 |
update_local_live_ranges ( |
| 683 |
vec_info *vinfo, |
| 684 |
hash_map<basic_block, vec<stmt_point>> &program_points_per_bb, |
| 685 |
hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb, |
| 686 |
machine_mode *biggest_mode) |
| 687 |
{ |
| 688 |
loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo); |
| 689 |
if (!loop_vinfo) |
| 690 |
return; |
| 691 |
|
| 692 |
class loop *loop = LOOP_VINFO_LOOP (loop_vinfo); |
| 693 |
basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo); |
| 694 |
unsigned int nbbs = loop->num_nodes; |
| 695 |
unsigned int i, j; |
| 696 |
gphi_iterator psi; |
| 697 |
gimple_stmt_iterator si; |
| 698 |
for (i = 0; i < nbbs; i++) |
| 699 |
{ |
| 700 |
basic_block bb = bbs[i]; |
| 701 |
if (dump_enabled_p ()) |
| 702 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 703 |
"Update local program points for bb %d:\n", |
| 704 |
bbs[i]->index); |
| 705 |
for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi)) |
| 706 |
{ |
| 707 |
gphi *phi = psi.phi (); |
| 708 |
stmt_vec_info stmt_info = vinfo->lookup_stmt (phi); |
| 709 |
if (STMT_VINFO_TYPE (vect_stmt_to_vectorize (stmt_info)) |
| 710 |
== undef_vec_info_type) |
| 711 |
continue; |
| 712 |
|
| 713 |
for (j = 0; j < gimple_phi_num_args (phi); j++) |
| 714 |
{ |
| 715 |
edge e = gimple_phi_arg_edge (phi, j); |
| 716 |
tree def = gimple_phi_arg_def (phi, j); |
| 717 |
auto *live_ranges = live_ranges_per_bb.get (bb); |
| 718 |
auto *live_range = live_ranges->get (def); |
| 719 |
if (poly_int_tree_p (def)) |
| 720 |
{ |
| 721 |
/* Insert live range of INTEGER_CST or POLY_CST since we will |
| 722 |
need to allocate a vector register for it. |
| 723 |
|
| 724 |
E.g. # j_17 = PHI <j_11(9), 0(5)> will be transformed |
| 725 |
into # vect_vec_iv_.8_24 = PHI <_25(9), { 0, ... }(5)> |
| 726 |
|
| 727 |
The live range for such value is short which only lives |
| 728 |
from program point 0 to 1. */ |
| 729 |
if (live_range) |
| 730 |
{ |
| 731 |
unsigned int start = (*live_range).first; |
| 732 |
(*live_range).first = 0; |
| 733 |
if (dump_enabled_p ()) |
| 734 |
dump_printf_loc ( |
| 735 |
MSG_NOTE, vect_location, |
| 736 |
"Update %T start point from %d to 0:\n", def, start); |
| 737 |
} |
| 738 |
else |
| 739 |
{ |
| 740 |
live_ranges->put (def, pair (0, 1)); |
| 741 |
auto &program_points = (*program_points_per_bb.get (bb)); |
| 742 |
if (program_points.is_empty ()) |
| 743 |
{ |
| 744 |
stmt_point info = {1, phi, stmt_info}; |
| 745 |
program_points.safe_push (info); |
| 746 |
} |
| 747 |
if (dump_enabled_p ()) |
| 748 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 749 |
"Add %T start point from 0 to 1:\n", |
| 750 |
def); |
| 751 |
} |
| 752 |
continue; |
| 753 |
} |
| 754 |
if (live_range && flow_bb_inside_loop_p (loop, e->src)) |
| 755 |
{ |
| 756 |
unsigned int start = (*live_range).first; |
| 757 |
(*live_range).first = 0; |
| 758 |
if (dump_enabled_p ()) |
| 759 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 760 |
"Update %T start point from %d to %d:\n", |
| 761 |
def, start, (*live_range).first); |
| 762 |
} |
| 763 |
live_ranges = live_ranges_per_bb.get (e->src); |
| 764 |
if (!program_points_per_bb.get (e->src)) |
| 765 |
continue; |
| 766 |
unsigned int max_point |
| 767 |
= (*program_points_per_bb.get (e->src)).length (); |
| 768 |
live_range = live_ranges->get (def); |
| 769 |
if (!live_range) |
| 770 |
continue; |
| 771 |
|
| 772 |
unsigned int end = (*live_range).second; |
| 773 |
(*live_range).second = max_point; |
| 774 |
if (dump_enabled_p ()) |
| 775 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 776 |
"Update %T end point from %d to %d:\n", def, |
| 777 |
end, (*live_range).second); |
| 778 |
} |
| 779 |
} |
| 780 |
for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) |
| 781 |
{ |
| 782 |
if (!is_gimple_assign_or_call (gsi_stmt (si))) |
| 783 |
continue; |
| 784 |
stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (si)); |
| 785 |
enum stmt_vec_info_type type |
| 786 |
= STMT_VINFO_TYPE (vect_stmt_to_vectorize (stmt_info)); |
| 787 |
if (need_additional_vector_vars_p (stmt_info)) |
| 788 |
{ |
| 789 |
/* For non-adjacent load/store STMT, we will potentially |
| 790 |
convert it into: |
| 791 |
|
| 792 |
1. MASK_LEN_GATHER_LOAD (..., perm indice). |
| 793 |
2. Contiguous load/store + VEC_PERM (..., perm indice) |
| 794 |
|
| 795 |
We will be likely using one more vector variable. */ |
| 796 |
unsigned int max_point |
| 797 |
= (*program_points_per_bb.get (bb)).length (); |
| 798 |
auto *live_ranges = live_ranges_per_bb.get (bb); |
| 799 |
bool existed_p = false; |
| 800 |
tree var = type == load_vec_info_type |
| 801 |
? gimple_get_lhs (gsi_stmt (si)) |
| 802 |
: get_store_value (gsi_stmt (si)); |
| 803 |
tree sel_type = build_nonstandard_integer_type ( |
| 804 |
TYPE_PRECISION (TREE_TYPE (var)), 1); |
| 805 |
*biggest_mode |
| 806 |
= get_biggest_mode (*biggest_mode, TYPE_MODE (sel_type)); |
| 807 |
tree sel = build_decl (UNKNOWN_LOCATION, VAR_DECL, |
| 808 |
get_identifier ("vect_perm"), sel_type); |
| 809 |
pair &live_range = live_ranges->get_or_insert (sel, &existed_p); |
| 810 |
gcc_assert (!existed_p); |
| 811 |
live_range = pair (0, max_point); |
| 812 |
if (dump_enabled_p ()) |
| 813 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 814 |
"Add perm indice %T, start = 0, end = %d\n", |
| 815 |
sel, max_point); |
| 816 |
if (!LOOP_VINFO_LENS (loop_vinfo).is_empty () |
| 817 |
&& LOOP_VINFO_LENS (loop_vinfo).length () > 1) |
| 818 |
{ |
| 819 |
/* If we are vectorizing a permutation when the rgroup number |
| 820 |
> 1, we will need additional mask to shuffle the second |
| 821 |
vector. */ |
| 822 |
tree mask = build_decl (UNKNOWN_LOCATION, VAR_DECL, |
| 823 |
get_identifier ("vect_perm_mask"), |
| 824 |
boolean_type_node); |
| 825 |
pair &live_range |
| 826 |
= live_ranges->get_or_insert (mask, &existed_p); |
| 827 |
gcc_assert (!existed_p); |
| 828 |
live_range = pair (0, max_point); |
| 829 |
if (dump_enabled_p ()) |
| 830 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 831 |
"Add perm mask %T, start = 0, end = %d\n", |
| 832 |
mask, max_point); |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
/* Compute the maximum live V_REGS. */ |
| 840 |
static bool |
| 841 |
has_unexpected_spills_p (loop_vec_info loop_vinfo) |
| 842 |
{ |
| 843 |
/* Compute local program points. |
| 844 |
It's a fast and effective computation. */ |
| 845 |
hash_map<basic_block, vec<stmt_point>> program_points_per_bb; |
| 846 |
compute_local_program_points (loop_vinfo, program_points_per_bb); |
| 847 |
|
| 848 |
/* Compute local live ranges. */ |
| 849 |
hash_map<basic_block, hash_map<tree, pair>> live_ranges_per_bb; |
| 850 |
machine_mode biggest_mode |
| 851 |
= compute_local_live_ranges (loop_vinfo, program_points_per_bb, |
| 852 |
live_ranges_per_bb); |
| 853 |
|
| 854 |
/* Update live ranges according to PHI. */ |
| 855 |
update_local_live_ranges (loop_vinfo, program_points_per_bb, |
| 856 |
live_ranges_per_bb, &biggest_mode); |
| 857 |
|
| 858 |
int lmul = compute_estimated_lmul (loop_vinfo, biggest_mode); |
| 859 |
gcc_assert (lmul <= RVV_M8); |
| 860 |
/* TODO: We calculate the maximum live vars base on current STMTS |
| 861 |
sequence. We can support live range shrink if it can give us |
| 862 |
big improvement in the future. */ |
| 863 |
if (lmul > RVV_M1) |
| 864 |
{ |
| 865 |
if (!live_ranges_per_bb.is_empty ()) |
| 866 |
{ |
| 867 |
unsigned int max_nregs = 0; |
| 868 |
for (hash_map<basic_block, hash_map<tree, pair>>::iterator iter |
| 869 |
= live_ranges_per_bb.begin (); |
| 870 |
iter != live_ranges_per_bb.end (); ++iter) |
| 871 |
{ |
| 872 |
basic_block bb = (*iter).first; |
| 873 |
unsigned int max_point |
| 874 |
= (*program_points_per_bb.get (bb)).length () + 1; |
| 875 |
if ((*iter).second.is_empty ()) |
| 876 |
continue; |
| 877 |
/* We prefer larger LMUL unless it causes register spillings. */ |
| 878 |
unsigned int nregs |
| 879 |
= max_number_of_live_regs (loop_vinfo, bb, (*iter).second, |
| 880 |
max_point, biggest_mode, lmul); |
| 881 |
if (nregs > max_nregs) |
| 882 |
max_nregs = nregs; |
| 883 |
} |
| 884 |
live_ranges_per_bb.empty (); |
| 885 |
if (max_nregs > V_REG_NUM) |
| 886 |
return true; |
| 887 |
} |
| 888 |
} |
| 889 |
if (!program_points_per_bb.is_empty ()) |
| 890 |
{ |
| 891 |
for (hash_map<basic_block, vec<stmt_point>>::iterator iter |
| 892 |
= program_points_per_bb.begin (); |
| 893 |
iter != program_points_per_bb.end (); ++iter) |
| 894 |
{ |
| 895 |
vec<stmt_point> program_points = (*iter).second; |
| 896 |
if (!program_points.is_empty ()) |
| 897 |
program_points.release (); |
| 898 |
} |
| 899 |
program_points_per_bb.empty (); |
| 900 |
} |
| 901 |
return false; |
| 902 |
} |
| 903 |
|
| 904 |
costs::costs (vec_info *vinfo, bool costing_for_scalar) |
| 905 |
: vector_costs (vinfo, costing_for_scalar) |
| 906 |
{ |
| 907 |
if (costing_for_scalar) |
| 908 |
m_cost_type = SCALAR_COST; |
| 909 |
else if (riscv_v_ext_vector_mode_p (vinfo->vector_mode)) |
| 910 |
m_cost_type = VLA_VECTOR_COST; |
| 911 |
else |
| 912 |
m_cost_type = VLS_VECTOR_COST; |
| 913 |
} |
| 914 |
|
| 915 |
/* Do one-time initialization of the costs given that we're |
| 916 |
costing the loop vectorization described by LOOP_VINFO. */ |
| 917 |
void |
| 918 |
costs::analyze_loop_vinfo (loop_vec_info loop_vinfo) |
| 919 |
{ |
| 920 |
/* Detect whether we're vectorizing for VLA and should apply the unrolling |
| 921 |
heuristic described above m_unrolled_vls_niters. */ |
| 922 |
record_potential_vls_unrolling (loop_vinfo); |
| 923 |
|
| 924 |
/* Detect whether the LOOP has unexpected spills. */ |
| 925 |
record_potential_unexpected_spills (loop_vinfo); |
| 926 |
} |
| 927 |
|
| 928 |
/* Analyze the vectorized program statements and use dynamic LMUL |
| 929 |
heuristic to detect whether the loop has unexpected spills. */ |
| 930 |
void |
| 931 |
costs::record_potential_unexpected_spills (loop_vec_info loop_vinfo) |
| 932 |
{ |
| 933 |
/* We only want to apply the heuristic if LOOP_VINFO is being |
| 934 |
vectorized for VLA and known NITERS VLS loop. */ |
| 935 |
if (rvv_max_lmul == RVV_DYNAMIC |
| 936 |
&& (m_cost_type == VLA_VECTOR_COST |
| 937 |
|| (m_cost_type == VLS_VECTOR_COST |
| 938 |
&& LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)))) |
| 939 |
{ |
| 940 |
bool post_dom_available_p = dom_info_available_p (CDI_POST_DOMINATORS); |
| 941 |
if (!post_dom_available_p) |
| 942 |
calculate_dominance_info (CDI_POST_DOMINATORS); |
| 943 |
m_has_unexpected_spills_p = has_unexpected_spills_p (loop_vinfo); |
| 944 |
if (!post_dom_available_p) |
| 945 |
free_dominance_info (CDI_POST_DOMINATORS); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
/* Decide whether to use the unrolling heuristic described above |
| 950 |
m_unrolled_vls_niters, updating that field if so. LOOP_VINFO |
| 951 |
describes the loop that we're vectorizing. */ |
| 952 |
void |
| 953 |
costs::record_potential_vls_unrolling (loop_vec_info loop_vinfo) |
| 954 |
{ |
| 955 |
/* We only want to apply the heuristic if LOOP_VINFO is being |
| 956 |
vectorized for VLA. */ |
| 957 |
if (m_cost_type != VLA_VECTOR_COST) |
| 958 |
return; |
| 959 |
|
| 960 |
/* We don't want to apply the heuristic to outer loops, since it's |
| 961 |
harder to track two levels of unrolling. */ |
| 962 |
if (LOOP_VINFO_LOOP (loop_vinfo)->inner) |
| 963 |
return; |
| 964 |
|
| 965 |
/* Only handle cases in which the number of VLS iterations |
| 966 |
would be known at compile time but the number of SVE iterations |
| 967 |
would not. */ |
| 968 |
if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) |
| 969 |
|| BYTES_PER_RISCV_VECTOR.is_constant ()) |
| 970 |
return; |
| 971 |
|
| 972 |
/* Guess how many times the VLS loop would iterate and make |
| 973 |
sure that it is within the complete unrolling limit. Even if the |
| 974 |
number of iterations is small enough, the number of statements might |
| 975 |
not be, which is why we need to estimate the number of statements too. */ |
| 976 |
unsigned int vls_vf = vect_vf_for_cost (loop_vinfo); |
| 977 |
unsigned HOST_WIDE_INT unrolled_vls_niters |
| 978 |
= LOOP_VINFO_INT_NITERS (loop_vinfo) / vls_vf; |
| 979 |
if (unrolled_vls_niters > (unsigned int) param_max_completely_peel_times) |
| 980 |
return; |
| 981 |
|
| 982 |
/* Record that we're applying the heuristic and should try to estimate |
| 983 |
the number of statements in the VLS loop. */ |
| 984 |
m_unrolled_vls_niters = unrolled_vls_niters; |
| 985 |
} |
| 986 |
|
| 987 |
/* Return true if (a) we're applying the VLS vs. VLA unrolling |
| 988 |
heuristic described above m_unrolled_vls_niters and (b) the heuristic |
| 989 |
says that we should prefer the VLS loop. */ |
| 990 |
bool |
| 991 |
costs::prefer_unrolled_loop () const |
| 992 |
{ |
| 993 |
if (!m_unrolled_vls_stmts) |
| 994 |
return false; |
| 995 |
|
| 996 |
if (dump_enabled_p ()) |
| 997 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 998 |
"Number of insns in" |
| 999 |
" unrolled VLS loop = " HOST_WIDE_INT_PRINT_UNSIGNED "\n", |
| 1000 |
m_unrolled_vls_stmts); |
| 1001 |
|
| 1002 |
/* The balance here is tricky. On the one hand, we can't be sure whether |
| 1003 |
the code is vectorizable with VLS or not. However, even if |
| 1004 |
it isn't vectorizable with VLS, there's a possibility that |
| 1005 |
the scalar code could also be unrolled. Some of the code might then |
| 1006 |
benefit from SLP, or from using LDP and STP. We therefore apply |
| 1007 |
the heuristic regardless of can_use_vls_p. */ |
| 1008 |
return (m_unrolled_vls_stmts |
| 1009 |
&& (m_unrolled_vls_stmts |
| 1010 |
<= (unsigned int) param_max_completely_peeled_insns)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
bool |
| 1014 |
costs::better_main_loop_than_p (const vector_costs *uncast_other) const |
| 1015 |
{ |
| 1016 |
auto other = static_cast<const costs *> (uncast_other); |
| 1017 |
auto this_loop_vinfo = as_a<loop_vec_info> (this->m_vinfo); |
| 1018 |
auto other_loop_vinfo = as_a<loop_vec_info> (other->m_vinfo); |
| 1019 |
|
| 1020 |
if (dump_enabled_p ()) |
| 1021 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 1022 |
"Comparing two main loops (%s at VF %d vs %s at VF %d)\n", |
| 1023 |
GET_MODE_NAME (this_loop_vinfo->vector_mode), |
| 1024 |
vect_vf_for_cost (this_loop_vinfo), |
| 1025 |
GET_MODE_NAME (other_loop_vinfo->vector_mode), |
| 1026 |
vect_vf_for_cost (other_loop_vinfo)); |
| 1027 |
|
| 1028 |
/* Apply the unrolling heuristic described above m_unrolled_vls_niters. */ |
| 1029 |
if (bool (m_unrolled_vls_stmts) != bool (other->m_unrolled_vls_stmts) |
| 1030 |
&& m_cost_type != other->m_cost_type) |
| 1031 |
{ |
| 1032 |
bool this_prefer_unrolled = this->prefer_unrolled_loop (); |
| 1033 |
bool other_prefer_unrolled = other->prefer_unrolled_loop (); |
| 1034 |
if (this_prefer_unrolled != other_prefer_unrolled) |
| 1035 |
{ |
| 1036 |
if (dump_enabled_p ()) |
| 1037 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 1038 |
"Preferring VLS loop because" |
| 1039 |
" it can be unrolled\n"); |
| 1040 |
return other_prefer_unrolled; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
else if (rvv_max_lmul == RVV_DYNAMIC) |
| 1044 |
{ |
| 1045 |
if (other->m_has_unexpected_spills_p) |
| 1046 |
{ |
| 1047 |
if (dump_enabled_p ()) |
| 1048 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 1049 |
"Preferring smaller LMUL loop because" |
| 1050 |
" it has unexpected spills\n"); |
| 1051 |
return true; |
| 1052 |
} |
| 1053 |
else if (riscv_v_ext_vector_mode_p (other_loop_vinfo->vector_mode)) |
| 1054 |
{ |
| 1055 |
if (LOOP_VINFO_NITERS_KNOWN_P (other_loop_vinfo)) |
| 1056 |
{ |
| 1057 |
if (maybe_gt (LOOP_VINFO_INT_NITERS (this_loop_vinfo), |
| 1058 |
LOOP_VINFO_VECT_FACTOR (this_loop_vinfo))) |
| 1059 |
{ |
| 1060 |
if (dump_enabled_p ()) |
| 1061 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 1062 |
"Keep current LMUL loop because" |
| 1063 |
" known NITERS exceed the new VF\n"); |
| 1064 |
return false; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
else |
| 1068 |
{ |
| 1069 |
if (dump_enabled_p ()) |
| 1070 |
dump_printf_loc (MSG_NOTE, vect_location, |
| 1071 |
"Keep current LMUL loop because" |
| 1072 |
" it is unknown NITERS\n"); |
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
/* If NITERS is unknown, we should not use VLS modes to vectorize |
| 1078 |
the loop since we don't support partial vectors for VLS modes, |
| 1079 |
that is, we will have full vectors (VLSmodes) on loop body |
| 1080 |
and partial vectors (VLAmodes) on loop epilogue which is very |
| 1081 |
inefficient. Instead, we should apply partial vectors (VLAmodes) |
| 1082 |
on loop body without an epilogue on unknown NITERS loop. */ |
| 1083 |
else if (!LOOP_VINFO_NITERS_KNOWN_P (this_loop_vinfo) |
| 1084 |
&& m_cost_type == VLS_VECTOR_COST) |
| 1085 |
return false; |
| 1086 |
|
| 1087 |
return vector_costs::better_main_loop_than_p (other); |
| 1088 |
} |
| 1089 |
|
| 1090 |
/* Returns the group size i.e. the number of vectors to be loaded by a |
| 1091 |
segmented load/store instruction. Return 0 if it is no segmented |
| 1092 |
load/store. */ |
| 1093 |
static int |
| 1094 |
segment_loadstore_group_size (enum vect_cost_for_stmt kind, |
| 1095 |
stmt_vec_info stmt_info) |
| 1096 |
{ |
| 1097 |
if (stmt_info |
| 1098 |
&& (kind == vector_load || kind == vector_store) |
| 1099 |
&& STMT_VINFO_DATA_REF (stmt_info)) |
| 1100 |
{ |
| 1101 |
stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info); |
| 1102 |
if (stmt_info |
| 1103 |
&& STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) == VMAT_LOAD_STORE_LANES) |
| 1104 |
return DR_GROUP_SIZE (stmt_info); |
| 1105 |
} |
| 1106 |
return 0; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* Adjust vectorization cost after calling riscv_builtin_vectorization_cost. |
| 1110 |
For some statement, we would like to further fine-grain tweak the cost on |
| 1111 |
top of riscv_builtin_vectorization_cost handling which doesn't have any |
| 1112 |
information on statement operation codes etc. */ |
| 1113 |
|
| 1114 |
unsigned |
| 1115 |
costs::adjust_stmt_cost (enum vect_cost_for_stmt kind, loop_vec_info loop, |
| 1116 |
stmt_vec_info stmt_info, |
| 1117 |
slp_tree, tree vectype, int stmt_cost) |
| 1118 |
{ |
| 1119 |
const cpu_vector_cost *costs = get_vector_costs (); |
| 1120 |
switch (kind) |
| 1121 |
{ |
| 1122 |
case scalar_to_vec: |
| 1123 |
stmt_cost += (FLOAT_TYPE_P (vectype) ? costs->regmove->FR2VR |
| 1124 |
: costs->regmove->GR2VR); |
| 1125 |
break; |
| 1126 |
case vec_to_scalar: |
| 1127 |
stmt_cost += (FLOAT_TYPE_P (vectype) ? costs->regmove->VR2FR |
| 1128 |
: costs->regmove->VR2GR); |
| 1129 |
break; |
| 1130 |
case vector_load: |
| 1131 |
case vector_store: |
| 1132 |
{ |
| 1133 |
if (stmt_info && stmt_info->stmt && STMT_VINFO_DATA_REF (stmt_info)) |
| 1134 |
{ |
| 1135 |
/* Segment loads and stores. When the group size is > 1 |
| 1136 |
the vectorizer will add a vector load/store statement for |
| 1137 |
each vector in the group. Here we additionally add permute |
| 1138 |
costs for each. */ |
| 1139 |
/* TODO: Indexed and ordered/unordered cost. */ |
| 1140 |
int group_size = segment_loadstore_group_size (kind, stmt_info); |
| 1141 |
if (group_size > 1) |
| 1142 |
{ |
| 1143 |
switch (group_size) |
| 1144 |
{ |
| 1145 |
case 2: |
| 1146 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1147 |
stmt_cost += costs->vla->segment_permute_2; |
| 1148 |
else |
| 1149 |
stmt_cost += costs->vls->segment_permute_2; |
| 1150 |
break; |
| 1151 |
case 3: |
| 1152 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1153 |
stmt_cost += costs->vla->segment_permute_3; |
| 1154 |
else |
| 1155 |
stmt_cost += costs->vls->segment_permute_3; |
| 1156 |
break; |
| 1157 |
case 4: |
| 1158 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1159 |
stmt_cost += costs->vla->segment_permute_4; |
| 1160 |
else |
| 1161 |
stmt_cost += costs->vls->segment_permute_4; |
| 1162 |
break; |
| 1163 |
case 5: |
| 1164 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1165 |
stmt_cost += costs->vla->segment_permute_5; |
| 1166 |
else |
| 1167 |
stmt_cost += costs->vls->segment_permute_5; |
| 1168 |
break; |
| 1169 |
case 6: |
| 1170 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1171 |
stmt_cost += costs->vla->segment_permute_6; |
| 1172 |
else |
| 1173 |
stmt_cost += costs->vls->segment_permute_6; |
| 1174 |
break; |
| 1175 |
case 7: |
| 1176 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1177 |
stmt_cost += costs->vla->segment_permute_7; |
| 1178 |
else |
| 1179 |
stmt_cost += costs->vls->segment_permute_7; |
| 1180 |
break; |
| 1181 |
case 8: |
| 1182 |
if (riscv_v_ext_vector_mode_p (loop->vector_mode)) |
| 1183 |
stmt_cost += costs->vla->segment_permute_8; |
| 1184 |
else |
| 1185 |
stmt_cost += costs->vls->segment_permute_8; |
| 1186 |
break; |
| 1187 |
default: |
| 1188 |
break; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
else |
| 1192 |
{ |
| 1193 |
/* Unit-stride vector loads and stores do not have offset |
| 1194 |
addressing as opposed to scalar loads and stores. |
| 1195 |
If the address depends on a variable we need an additional |
| 1196 |
add/sub for each load/store in the worst case. */ |
| 1197 |
data_reference *dr = STMT_VINFO_DATA_REF (stmt_info); |
| 1198 |
class loop *father = stmt_info->stmt->bb->loop_father; |
| 1199 |
if (!loop && father && !father->inner && father->superloops) |
| 1200 |
{ |
| 1201 |
tree ref; |
| 1202 |
if (TREE_CODE (dr->ref) != MEM_REF |
| 1203 |
|| !(ref = TREE_OPERAND (dr->ref, 0)) |
| 1204 |
|| TREE_CODE (ref) != SSA_NAME) |
| 1205 |
break; |
| 1206 |
|
| 1207 |
if (SSA_NAME_IS_DEFAULT_DEF (ref)) |
| 1208 |
break; |
| 1209 |
|
| 1210 |
if (memrefs.contains ({ref, cst0})) |
| 1211 |
break; |
| 1212 |
|
| 1213 |
memrefs.add ({ref, cst0}); |
| 1214 |
|
| 1215 |
/* In case we have not seen REF before and the base |
| 1216 |
address is a pointer operation try a bit harder. */ |
| 1217 |
tree base = DR_BASE_ADDRESS (dr); |
| 1218 |
if (TREE_CODE (base) == POINTER_PLUS_EXPR |
| 1219 |
|| TREE_CODE (base) == POINTER_DIFF_EXPR) |
| 1220 |
{ |
| 1221 |
/* Deconstruct BASE's first operand. If it is a |
| 1222 |
binary operation, i.e. a base and an "offset" |
| 1223 |
store this pair. Only increase the stmt_cost if |
| 1224 |
we haven't seen it before. */ |
| 1225 |
tree argp = TREE_OPERAND (base, 1); |
| 1226 |
typedef std::pair<tree, tree> addr_pair; |
| 1227 |
addr_pair pair; |
| 1228 |
if (TREE_CODE_CLASS (TREE_CODE (argp)) == tcc_binary) |
| 1229 |
{ |
| 1230 |
tree argp0 = tree_strip_nop_conversions |
| 1231 |
(TREE_OPERAND (argp, 0)); |
| 1232 |
tree argp1 = TREE_OPERAND (argp, 1); |
| 1233 |
pair = addr_pair (argp0, argp1); |
| 1234 |
if (memrefs.contains (pair)) |
| 1235 |
break; |
| 1236 |
|
| 1237 |
memrefs.add (pair); |
| 1238 |
stmt_cost |
| 1239 |
+= builtin_vectorization_cost (scalar_stmt, |
| 1240 |
NULL_TREE, 0); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
break; |
| 1247 |
} |
| 1248 |
|
| 1249 |
default: |
| 1250 |
break; |
| 1251 |
} |
| 1252 |
return stmt_cost; |
| 1253 |
} |
| 1254 |
|
| 1255 |
unsigned |
| 1256 |
costs::add_stmt_cost (int count, vect_cost_for_stmt kind, |
| 1257 |
stmt_vec_info stmt_info, slp_tree node, tree vectype, |
| 1258 |
int misalign, vect_cost_model_location where) |
| 1259 |
{ |
| 1260 |
int stmt_cost |
| 1261 |
= targetm.vectorize.builtin_vectorization_cost (kind, vectype, misalign); |
| 1262 |
|
| 1263 |
/* Do one-time initialization based on the vinfo. */ |
| 1264 |
loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo); |
| 1265 |
if (!m_analyzed_vinfo) |
| 1266 |
{ |
| 1267 |
if (loop_vinfo) |
| 1268 |
analyze_loop_vinfo (loop_vinfo); |
| 1269 |
|
| 1270 |
memrefs.empty (); |
| 1271 |
m_analyzed_vinfo = true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (stmt_info) |
| 1275 |
{ |
| 1276 |
/* If we're applying the VLA vs. VLS unrolling heuristic, |
| 1277 |
estimate the number of statements in the unrolled VLS |
| 1278 |
loop. For simplicity, we assume that one iteration of the |
| 1279 |
VLS loop would need the same number of statements |
| 1280 |
as one iteration of the VLA loop. */ |
| 1281 |
if (where == vect_body && m_unrolled_vls_niters) |
| 1282 |
m_unrolled_vls_stmts += count * m_unrolled_vls_niters; |
| 1283 |
} |
| 1284 |
|
| 1285 |
if (vectype) |
| 1286 |
stmt_cost = adjust_stmt_cost (kind, loop_vinfo, stmt_info, node, vectype, |
| 1287 |
stmt_cost); |
| 1288 |
|
| 1289 |
return record_stmt_cost (stmt_info, where, count * stmt_cost); |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* For some target specific vectorization cost which can't be handled per stmt, |
| 1293 |
we check the requisite conditions and adjust the vectorization cost |
| 1294 |
accordingly if satisfied. One typical example is to model and adjust |
| 1295 |
loop_len cost for known_lt (NITERS, VF). */ |
| 1296 |
|
| 1297 |
void |
| 1298 |
costs::adjust_vect_cost_per_loop (loop_vec_info loop_vinfo) |
| 1299 |
{ |
| 1300 |
if (LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo) |
| 1301 |
&& !LOOP_VINFO_USING_DECREMENTING_IV_P (loop_vinfo)) |
| 1302 |
{ |
| 1303 |
/* In middle-end loop vectorizer, we don't count the loop_len cost in |
| 1304 |
vect_estimate_min_profitable_iters when NITERS < VF, that is, we only |
| 1305 |
count cost of len that we need to iterate loop more than once with VF. |
| 1306 |
It's correct for most of the cases: |
| 1307 |
|
| 1308 |
E.g. VF = [4, 4] |
| 1309 |
for (int i = 0; i < 3; i ++) |
| 1310 |
a[i] += b[i]; |
| 1311 |
|
| 1312 |
We don't need to cost MIN_EXPR or SELECT_VL for the case above. |
| 1313 |
|
| 1314 |
However, for some inefficient vectorized cases, it does use MIN_EXPR |
| 1315 |
to generate len. |
| 1316 |
|
| 1317 |
E.g. VF = [256, 256] |
| 1318 |
|
| 1319 |
Loop body: |
| 1320 |
# loop_len_110 = PHI <18(2), _119(11)> |
| 1321 |
... |
| 1322 |
_117 = MIN_EXPR <ivtmp_114, 18>; |
| 1323 |
_118 = 18 - _117; |
| 1324 |
_119 = MIN_EXPR <_118, POLY_INT_CST [256, 256]>; |
| 1325 |
... |
| 1326 |
|
| 1327 |
Epilogue: |
| 1328 |
... |
| 1329 |
_112 = .VEC_EXTRACT (vect_patt_27.14_109, _111); |
| 1330 |
|
| 1331 |
We cost 1 unconditionally for this situation like other targets which |
| 1332 |
apply mask as the loop control. */ |
| 1333 |
rgroup_controls *rgc; |
| 1334 |
unsigned int num_vectors_m1; |
| 1335 |
unsigned int body_stmts = 0; |
| 1336 |
FOR_EACH_VEC_ELT (LOOP_VINFO_LENS (loop_vinfo), num_vectors_m1, rgc) |
| 1337 |
if (rgc->type) |
| 1338 |
body_stmts += num_vectors_m1 + 1; |
| 1339 |
|
| 1340 |
add_stmt_cost (body_stmts, scalar_stmt, NULL, NULL, NULL_TREE, 0, |
| 1341 |
vect_body); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
void |
| 1346 |
costs::finish_cost (const vector_costs *scalar_costs) |
| 1347 |
{ |
| 1348 |
if (loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo)) |
| 1349 |
{ |
| 1350 |
adjust_vect_cost_per_loop (loop_vinfo); |
| 1351 |
} |
| 1352 |
vector_costs::finish_cost (scalar_costs); |
| 1353 |
} |
| 1354 |
|
| 1355 |
} // namespace riscv_vector |