Debian Codegraph @

gcc/config/riscv/riscv-vsetvl.cc

raw
1 /* VSETVL pass for RISC-V 'V' Extension for GNU compiler.
2 Copyright (C) 2022-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 /* The values of the vl and vtype registers will affect the behavior of RVV
22 insns. That is, when we need to execute an RVV instruction, we need to set
23 the correct vl and vtype values by executing the vsetvl instruction before.
24 Executing the fewest number of vsetvl instructions while keeping the behavior
25 the same is the problem this pass is trying to solve. This vsetvl pass is
26 divided into 5 phases:
27
28 - Phase 1 (fuse local vsetvl infos): traverses each Basic Block, parses
29 each instruction in it that affects vl and vtype state and generates an
30 array of vsetvl_info objects. Then traverse the vsetvl_info array from
31 front to back and perform fusion according to the fusion rules. The fused
32 vsetvl infos are stored in the vsetvl_block_info object's `infos` field.
33
34 - Phase 2 (earliest fuse global vsetvl infos): The header_info and
35 footer_info of vsetvl_block_info are used as expressions, and the
36 earliest of each expression is computed. Based on the earliest
37 information, try to lift up the corresponding vsetvl info to the src
38 basic block of the edge (mainly to reduce the total number of vsetvl
39 instructions, this uplift will cause some execution paths to execute
40 vsetvl instructions that shouldn't be there).
41
42 - Phase 3 (pre global vsetvl info): The header_info and footer_info of
43 vsetvl_block_info are used as expressions, and the LCM algorithm is used
44 to compute the header_info that needs to be deleted and the one that
45 needs to be inserted in some edges.
46
47 - Phase 4 (emit vsetvl insns) : Based on the fusion result of Phase 1 and
48 the deletion and insertion information of Phase 3, the mandatory vsetvl
49 instruction insertion, modification and deletion are performed.
50
51 - Phase 5 (cleanup): Clean up the avl operand in the RVV operator
52 instruction and cleanup the unused dest operand of the vsetvl insn.
53
54 After the Phase 1 a virtual CFG of vsetvl_info is generated. The virtual
55 basic block is represented by vsetvl_block_info, and the virtual vsetvl
56 statements inside are represented by vsetvl_info. The later phases 2 and 3
57 are constantly modifying and adjusting this virtual CFG. Phase 4 performs
58 insertion, modification and deletion of vsetvl instructions based on the
59 optimized virtual CFG. The Phase 1, 2 and 3 do not involve modifications to
60 the RTL.
61 */
62
63 #define IN_TARGET_CODE 1
64 #define INCLUDE_ALGORITHM
65 #define INCLUDE_FUNCTIONAL
66 #define INCLUDE_ARRAY
67
68 #include "config.h"
69 #include "system.h"
70 #include "coretypes.h"
71 #include "tm.h"
72 #include "backend.h"
73 #include "rtl.h"
74 #include "target.h"
75 #include "tree-pass.h"
76 #include "df.h"
77 #include "rtl-ssa.h"
78 #include "cfgcleanup.h"
79 #include "insn-config.h"
80 #include "insn-attr.h"
81 #include "insn-opinit.h"
82 #include "tm-constrs.h"
83 #include "cfgrtl.h"
84 #include "cfganal.h"
85 #include "lcm.h"
86 #include "predict.h"
87 #include "profile-count.h"
88 #include "gcse.h"
89 #include "cfgloop.h"
90
91 using namespace rtl_ssa;
92 using namespace riscv_vector;
93
94 /* Set the bitmap DST to the union of SRC of predecessors of
95 basic block B.
96 It's a bit different from bitmap_union_of_preds in cfganal.cc. This function
97 takes into account the case where pred is ENTRY basic block. The main reason
98 for this difference is to make it easier to insert some special value into
99 the ENTRY base block. For example, vsetvl_info with a status of UNKNOWN. */
100 static void
101 bitmap_union_of_preds_with_entry (sbitmap dst, sbitmap *src, basic_block b)
102 {
103 unsigned int set_size = dst->size;
104 edge e;
105 unsigned ix;
106
107 for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
108 {
109 e = EDGE_PRED (b, ix);
110 bitmap_copy (dst, src[e->src->index]);
111 break;
112 }
113
114 if (ix == EDGE_COUNT (b->preds))
115 bitmap_clear (dst);
116 else
117 for (ix++; ix < EDGE_COUNT (b->preds); ix++)
118 {
119 unsigned int i;
120 SBITMAP_ELT_TYPE *p, *r;
121
122 e = EDGE_PRED (b, ix);
123 p = src[e->src->index]->elms;
124 r = dst->elms;
125 for (i = 0; i < set_size; i++)
126 *r++ |= *p++;
127 }
128 }
129
130 /* Compute the reaching definition in and out based on the gen and KILL
131 information's in each Base Blocks.
132 This function references the compute_available implementation in lcm.cc */
133 static void
134 compute_reaching_definition (sbitmap *gen, sbitmap *kill, sbitmap *in,
135 sbitmap *out)
136 {
137 edge e;
138 basic_block *worklist, *qin, *qout, *qend, bb;
139 unsigned int qlen;
140 edge_iterator ei;
141
142 /* Allocate a worklist array/queue. Entries are only added to the
143 list if they were not already on the list. So the size is
144 bounded by the number of basic blocks. */
145 qin = qout = worklist
146 = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
147
148 /* Put every block on the worklist; this is necessary because of the
149 optimistic initialization of AVOUT above. Use reverse postorder
150 to make the forward dataflow problem require less iterations. */
151 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
152 int n = pre_and_rev_post_order_compute_fn (cfun, NULL, rpo, false);
153 for (int i = 0; i < n; ++i)
154 {
155 bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
156 *qin++ = bb;
157 bb->aux = bb;
158 }
159 free (rpo);
160
161 qin = worklist;
162 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
163 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
164
165 /* Mark blocks which are successors of the entry block so that we
166 can easily identify them below. */
167 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
168 e->dest->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun);
169
170 /* Iterate until the worklist is empty. */
171 while (qlen)
172 {
173 /* Take the first entry off the worklist. */
174 bb = *qout++;
175 qlen--;
176
177 if (qout >= qend)
178 qout = worklist;
179
180 /* Do not clear the aux field for blocks which are successors of the
181 ENTRY block. That way we never add then to the worklist again. */
182 if (bb->aux != ENTRY_BLOCK_PTR_FOR_FN (cfun))
183 bb->aux = NULL;
184
185 bitmap_union_of_preds_with_entry (in[bb->index], out, bb);
186
187 if (bitmap_ior_and_compl (out[bb->index], gen[bb->index], in[bb->index],
188 kill[bb->index]))
189 /* If the out state of this block changed, then we need
190 to add the successors of this block to the worklist
191 if they are not already on the worklist. */
192 FOR_EACH_EDGE (e, ei, bb->succs)
193 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
194 {
195 *qin++ = e->dest;
196 e->dest->aux = e;
197 qlen++;
198
199 if (qin >= qend)
200 qin = worklist;
201 }
202 }
203
204 clear_aux_for_edges ();
205 clear_aux_for_blocks ();
206 free (worklist);
207 }
208
209 /* Classification of vsetvl instruction. */
210 enum vsetvl_type
211 {
212 VSETVL_NORMAL,
213 VSETVL_VTYPE_CHANGE_ONLY,
214 VSETVL_DISCARD_RESULT,
215 NUM_VSETVL_TYPE
216 };
217
218 enum emit_type
219 {
220 /* emit_insn directly. */
221 EMIT_DIRECT,
222 EMIT_BEFORE,
223 EMIT_AFTER,
224 };
225
226 static const int MAX_LMUL = 8;
227
228 /* dump helper functions */
229 static const char *
230 vlmul_to_str (vlmul_type vlmul)
231 {
232 switch (vlmul)
233 {
234 case LMUL_1:
235 return "m1";
236 case LMUL_2:
237 return "m2";
238 case LMUL_4:
239 return "m4";
240 case LMUL_8:
241 return "m8";
242 case LMUL_RESERVED:
243 return "INVALID LMUL";
244 case LMUL_F8:
245 return "mf8";
246 case LMUL_F4:
247 return "mf4";
248 case LMUL_F2:
249 return "mf2";
250
251 default:
252 gcc_unreachable ();
253 }
254 }
255
256 static const char *
257 policy_to_str (bool agnostic_p)
258 {
259 return agnostic_p ? "agnostic" : "undisturbed";
260 }
261
262 /* Return true if it is an RVV instruction depends on VTYPE global
263 status register. */
264 static bool
265 has_vtype_op (rtx_insn *rinsn)
266 {
267 return recog_memoized (rinsn) >= 0 && get_attr_has_vtype_op (rinsn);
268 }
269
270 /* Return true if the instruction ignores VLMUL field of VTYPE. */
271 static bool
272 ignore_vlmul_insn_p (rtx_insn *rinsn)
273 {
274 return get_attr_type (rinsn) == TYPE_VIMOVVX
275 || get_attr_type (rinsn) == TYPE_VFMOVVF
276 || get_attr_type (rinsn) == TYPE_VIMOVXV
277 || get_attr_type (rinsn) == TYPE_VFMOVFV;
278 }
279
280 /* Return true if the instruction is scalar move instruction. */
281 static bool
282 scalar_move_insn_p (rtx_insn *rinsn)
283 {
284 return get_attr_type (rinsn) == TYPE_VIMOVXV
285 || get_attr_type (rinsn) == TYPE_VFMOVFV;
286 }
287
288 /* Return true if the instruction is fault first load instruction. */
289 static bool
290 fault_first_load_p (rtx_insn *rinsn)
291 {
292 return recog_memoized (rinsn) >= 0
293 && (get_attr_type (rinsn) == TYPE_VLDFF
294 || get_attr_type (rinsn) == TYPE_VLSEGDFF);
295 }
296
297 /* Return the VL output register from a fault-only-first load with VL
298 output (pred_fault_load_set_vl pattern) if RINSN is such an insn
299 or NULL_RTX otherwise.
300 The pattern has: (set vl_output (unspec:P [(reg:SI VL_REGNUM)]
301 UNSPEC_READ_VL)) */
302 static rtx
303 get_fof_set_vl_reg (rtx_insn *rinsn)
304 {
305 if (!fault_first_load_p (rinsn))
306 return NULL_RTX;
307
308 rtx pat = PATTERN (rinsn);
309 if (GET_CODE (pat) != PARALLEL)
310 return NULL_RTX;
311
312 if (XVECLEN (pat, 0) != 3)
313 return NULL_RTX;
314
315 rtx sub = XVECEXP (pat, 0, 2);
316 if (GET_CODE (sub) == SET
317 && GET_CODE (SET_SRC (sub)) == UNSPEC
318 && XINT (SET_SRC (sub), 1) == UNSPEC_READ_VL)
319 return SET_DEST (sub);
320
321 return NULL_RTX;
322 }
323
324 /* Initialize RTL SSA and related infrastructure for vsetvl analysis. */
325 static void
326 init_rtl_ssa ()
327 {
328 calculate_dominance_info (CDI_DOMINATORS);
329 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
330 connect_infinite_loops_to_exit ();
331 df_analyze ();
332 crtl->ssa = new function_info (cfun);
333 }
334
335 /* Finalize RTL SSA and cleanup. */
336 static void
337 finish_rtl_ssa ()
338 {
339 free_dominance_info (CDI_DOMINATORS);
340 loop_optimizer_finalize ();
341 if (crtl->ssa->perform_pending_updates ())
342 cleanup_cfg (0);
343 delete crtl->ssa;
344 crtl->ssa = nullptr;
345 }
346
347 /* Emit read_vl instructions after fault-only-first loads that have
348 a VL output register.
349 This needs to happen last, i.e. when we made the VL dataflow
350 explicit by inserting vsetvls. */
351
352 static void
353 emit_fof_read_vls ()
354 {
355 basic_block bb;
356 rtx_insn *rinsn;
357
358 FOR_EACH_BB_FN (bb, cfun)
359 FOR_BB_INSNS (bb, rinsn)
360 {
361 if (!NONDEBUG_INSN_P (rinsn))
362 continue;
363
364 rtx vl_dest = get_fof_set_vl_reg (rinsn);
365 if (!vl_dest)
366 continue;
367
368 if (dump_file)
369 fprintf (dump_file,
370 " Inserting read_vl after FoF insn %d into r%d\n",
371 INSN_UID (rinsn), REGNO (vl_dest));
372
373 rtx read_vl_pat = gen_read_vl (Pmode, vl_dest);
374 emit_insn_after (read_vl_pat, rinsn);
375 }
376 }
377
378 /* Return true if the instruction is read vl instruction. */
379 static bool
380 read_vl_insn_p (rtx_insn *rinsn)
381 {
382 return recog_memoized (rinsn) >= 0 && get_attr_type (rinsn) == TYPE_RDVL;
383 }
384
385 /* Return true if it is a vsetvl instruction. */
386 static bool
387 vector_config_insn_p (rtx_insn *rinsn)
388 {
389 return recog_memoized (rinsn) >= 0 && get_attr_type (rinsn) == TYPE_VSETVL;
390 }
391
392 /* Return true if it is vsetvldi or vsetvlsi. */
393 static bool
394 vsetvl_insn_p (rtx_insn *rinsn)
395 {
396 if (!rinsn || !vector_config_insn_p (rinsn))
397 return false;
398 return (INSN_CODE (rinsn) == CODE_FOR_vsetvldi
399 || INSN_CODE (rinsn) == CODE_FOR_vsetvlsi);
400 }
401
402 /* Return true if it is the bogus vsetvl_pre instruction:
403
404 (define_insn "@vlmax_avl<mode>"
405 [(set (match_operand:P 0 "register_operand" "=r")
406 (unspec:P [(match_operand:P 1 "const_int_operand" "i")] UNSPEC_VLMAX))]
407 "TARGET_VECTOR"
408 ""
409 [(set_attr "type" "vsetvl_pre")])
410
411 As described above, it's the bogus instruction which doesn't any assembler
412 and should be removed eventually. It's used for occupying a scalar register
413 for VLMAX avl RVV instruction before register allocation.
414
415 Before RA:
416
417 ...
418 vsetvl_pre (set r136)
419 vadd.vv (use r136 with VLMAX avl)
420 ...
421
422 After RA:
423
424 ...
425 vsetvl_pre (set a5)
426 vadd.vv (use r136 with VLMAX avl)
427 ...
428
429 VSETVL PASS:
430
431 ...
432 vsetvl_pre (set a5) ---> removed.
433 vsetvl a5,zero,... ---> Inserted.
434 vadd.vv
435 ...
436 */
437 static bool
438 vsetvl_pre_insn_p (rtx_insn *rinsn)
439 {
440 return recog_memoized (rinsn) >= 0
441 && get_attr_type (rinsn) == TYPE_VSETVL_PRE;
442 }
443
444 /* Return true if it is vsetvl zero, rs1. */
445 static bool
446 vsetvl_discard_result_insn_p (rtx_insn *rinsn)
447 {
448 if (!vector_config_insn_p (rinsn))
449 return false;
450 return (INSN_CODE (rinsn) == CODE_FOR_vsetvl_discard_resultdi
451 || INSN_CODE (rinsn) == CODE_FOR_vsetvl_discard_resultsi);
452 }
453
454 static bool
455 real_insn_and_same_bb_p (const insn_info *insn, const bb_info *bb)
456 {
457 return insn != nullptr && insn->is_real () && insn->bb () == bb;
458 }
459
460 /* Helper function to get VL operand for VLMAX insn. */
461 static rtx
462 get_vl (rtx_insn *rinsn)
463 {
464 if (has_vl_op (rinsn))
465 {
466 extract_insn_cached (rinsn);
467 return recog_data.operand[get_attr_vl_op_idx (rinsn)];
468 }
469 return SET_DEST (XVECEXP (PATTERN (rinsn), 0, 0));
470 }
471
472 /* Helper function to get AVL operand. */
473 static rtx
474 get_avl (rtx_insn *rinsn)
475 {
476 if (vsetvl_insn_p (rinsn) || vsetvl_discard_result_insn_p (rinsn))
477 return XVECEXP (SET_SRC (XVECEXP (PATTERN (rinsn), 0, 0)), 0, 0);
478
479 if (!has_vl_op (rinsn))
480 return NULL_RTX;
481 if (vlmax_avl_type_p (rinsn))
482 return RVV_VLMAX;
483 extract_insn_cached (rinsn);
484 return recog_data.operand[get_attr_vl_op_idx (rinsn)];
485 }
486
487 /* Get default mask policy. */
488 static bool
489 get_default_ma ()
490 {
491 /* For the instruction that doesn't require MA, we still need a default value
492 to emit vsetvl. We pick up the default value according to prefer policy. */
493 return (bool) (get_prefer_mask_policy () & 0x1
494 || (get_prefer_mask_policy () >> 1 & 0x1));
495 }
496
497 /* Helper function to get MA operand. */
498 static bool
499 mask_agnostic_p (rtx_insn *rinsn)
500 {
501 /* If it doesn't have MA, we return agnostic by default. */
502 extract_insn_cached (rinsn);
503 int ma = get_attr_ma (rinsn);
504 return ma == INVALID_ATTRIBUTE ? get_default_ma () : IS_AGNOSTIC (ma);
505 }
506
507 /* Return true if FN has a vector instruction that use VL/VTYPE. */
508 static bool
509 has_vector_insn (function *fn)
510 {
511 basic_block cfg_bb;
512 rtx_insn *rinsn;
513 FOR_ALL_BB_FN (cfg_bb, fn)
514 FOR_BB_INSNS (cfg_bb, rinsn)
515 if (NONDEBUG_INSN_P (rinsn) && has_vtype_op (rinsn))
516 return true;
517 return false;
518 }
519
520 static vlmul_type
521 calculate_vlmul (unsigned int sew, unsigned int ratio)
522 {
523 const vlmul_type ALL_LMUL[]
524 = {LMUL_1, LMUL_2, LMUL_4, LMUL_8, LMUL_F8, LMUL_F4, LMUL_F2};
525 for (const vlmul_type vlmul : ALL_LMUL)
526 if (calculate_ratio (sew, vlmul) == ratio)
527 return vlmul;
528 return LMUL_RESERVED;
529 }
530
531 /* Get the currently supported maximum sew used in the int rvv instructions. */
532 static uint8_t
533 get_max_int_sew ()
534 {
535 if (TARGET_VECTOR_ELEN_64)
536 return 64;
537 else if (TARGET_VECTOR_ELEN_32)
538 return 32;
539 gcc_unreachable ();
540 }
541
542 /* Get the currently supported maximum sew used in the float rvv instructions.
543 */
544 static uint8_t
545 get_max_float_sew ()
546 {
547 if (TARGET_VECTOR_ELEN_FP_64)
548 return 64;
549 else if (TARGET_VECTOR_ELEN_FP_32)
550 return 32;
551 else if (TARGET_VECTOR_ELEN_FP_16)
552 return 16;
553 gcc_unreachable ();
554 }
555
556 enum def_type
557 {
558 REAL_SET = 1 << 0,
559 PHI_SET = 1 << 1,
560 BB_HEAD_SET = 1 << 2,
561 BB_END_SET = 1 << 3,
562 /* ??? TODO: In RTL_SSA framework, we have REAL_SET,
563 PHI_SET, BB_HEAD_SET, BB_END_SET and
564 CLOBBER_DEF def_info types. Currently,
565 we conservatively do not optimize clobber
566 def since we don't see the case that we
567 need to optimize it. */
568 CLOBBER_DEF = 1 << 4
569 };
570
571 static bool
572 insn_should_be_added_p (const insn_info *insn, unsigned int types)
573 {
574 if (insn->is_real () && (types & REAL_SET))
575 return true;
576 if (insn->is_phi () && (types & PHI_SET))
577 return true;
578 if (insn->is_bb_head () && (types & BB_HEAD_SET))
579 return true;
580 if (insn->is_bb_end () && (types & BB_END_SET))
581 return true;
582 return false;
583 }
584
585 static const hash_set<use_info *>
586 get_all_real_uses (insn_info *insn, unsigned regno)
587 {
588 gcc_assert (insn->is_real ());
589
590 hash_set<use_info *> uses;
591 auto_vec<phi_info *> work_list;
592 hash_set<phi_info *> visited_list;
593
594 for (def_info *def : insn->defs ())
595 {
596 if (!def->is_reg () || def->regno () != regno)
597 continue;
598 set_info *set = safe_dyn_cast<set_info *> (def);
599 if (!set)
600 continue;
601 for (use_info *use : set->nondebug_insn_uses ())
602 if (use->insn ()->is_real ())
603 uses.add (use);
604 for (use_info *use : set->phi_uses ())
605 work_list.safe_push (use->phi ());
606 }
607
608 while (!work_list.is_empty ())
609 {
610 phi_info *phi = work_list.pop ();
611 visited_list.add (phi);
612
613 for (use_info *use : phi->nondebug_insn_uses ())
614 if (use->insn ()->is_real ())
615 uses.add (use);
616 for (use_info *use : phi->phi_uses ())
617 if (!visited_list.contains (use->phi ()))
618 work_list.safe_push (use->phi ());
619 }
620 return uses;
621 }
622
623 /* Recursively find all define instructions. The kind of instruction is
624 specified by the DEF_TYPE. */
625 static hash_set<set_info *>
626 get_all_sets (phi_info *phi, unsigned int types)
627 {
628 hash_set<set_info *> insns;
629 auto_vec<phi_info *> work_list;
630 hash_set<phi_info *> visited_list;
631 if (!phi)
632 return hash_set<set_info *> ();
633 work_list.safe_push (phi);
634
635 while (!work_list.is_empty ())
636 {
637 phi_info *phi = work_list.pop ();
638 visited_list.add (phi);
639 for (use_info *use : phi->inputs ())
640 {
641 def_info *def = use->def ();
642 set_info *set = safe_dyn_cast<set_info *> (def);
643 if (!set)
644 return hash_set<set_info *> ();
645
646 gcc_assert (!set->insn ()->is_debug_insn ());
647
648 if (insn_should_be_added_p (set->insn (), types))
649 insns.add (set);
650 if (set->insn ()->is_phi ())
651 {
652 phi_info *new_phi = as_a<phi_info *> (set);
653 if (!visited_list.contains (new_phi))
654 work_list.safe_push (new_phi);
655 }
656 }
657 }
658 return insns;
659 }
660
661 static hash_set<set_info *>
662 get_all_sets (set_info *set, bool /* get_real_inst */ real_p,
663 bool /*get_phi*/ phi_p, bool /* get_function_parameter*/ param_p)
664 {
665 if (real_p && phi_p && param_p)
666 return get_all_sets (safe_dyn_cast<phi_info *> (set),
667 REAL_SET | PHI_SET | BB_HEAD_SET | BB_END_SET);
668
669 else if (real_p && param_p)
670 return get_all_sets (safe_dyn_cast<phi_info *> (set),
671 REAL_SET | BB_HEAD_SET | BB_END_SET);
672
673 else if (real_p)
674 return get_all_sets (safe_dyn_cast<phi_info *> (set), REAL_SET);
675 return hash_set<set_info *> ();
676 }
677
678 static bool
679 source_equal_p (insn_info *insn1, insn_info *insn2)
680 {
681 if (!insn1 || !insn2)
682 return false;
683 rtx_insn *rinsn1 = insn1->rtl ();
684 rtx_insn *rinsn2 = insn2->rtl ();
685 if (!rinsn1 || !rinsn2)
686 return false;
687
688 rtx note1 = find_reg_equal_equiv_note (rinsn1);
689 rtx note2 = find_reg_equal_equiv_note (rinsn2);
690 /* We could handle the case of similar-looking REG_EQUALs as well but
691 would need to verify that no insn in between modifies any of the source
692 operands. */
693 if (note1 && note2 && rtx_equal_p (note1, note2)
694 && REG_NOTE_KIND (note1) == REG_EQUIV)
695 return true;
696 return false;
697 }
698
699 static insn_info *
700 extract_single_source (set_info *set)
701 {
702 if (!set)
703 return nullptr;
704 if (set->insn ()->is_real ())
705 return set->insn ();
706 if (!set->insn ()->is_phi ())
707 return nullptr;
708 hash_set<set_info *> sets = get_all_sets (set, true, false, true);
709 if (sets.is_empty ())
710 return nullptr;
711
712 insn_info *first_insn = (*sets.begin ())->insn ();
713 if (first_insn->is_artificial ())
714 return nullptr;
715 for (const set_info *set : sets)
716 {
717 /* If there is a head or end insn, we conservative return
718 NULL so that VSETVL PASS will insert vsetvl directly. */
719 if (set->insn ()->is_artificial ())
720 return nullptr;
721 if (set != *sets.begin () && !source_equal_p (set->insn (), first_insn))
722 return nullptr;
723 }
724
725 return first_insn;
726 }
727
728 static bool
729 same_equiv_note_p (set_info *set1, set_info *set2)
730 {
731 insn_info *insn1 = extract_single_source (set1);
732 insn_info *insn2 = extract_single_source (set2);
733 if (!insn1 || !insn2)
734 return false;
735 return source_equal_p (insn1, insn2);
736 }
737
738 /* Return true if the SET result is not used by any instructions. */
739 static bool
740 has_no_uses (basic_block cfg_bb, rtx_insn *rinsn, int regno)
741 {
742 if (bitmap_bit_p (df_get_live_out (cfg_bb), regno))
743 return false;
744
745 rtx_insn *iter;
746 for (iter = NEXT_INSN (rinsn); iter && iter != NEXT_INSN (BB_END (cfg_bb));
747 iter = NEXT_INSN (iter))
748 if (df_find_use (iter, regno_reg_rtx[regno]))
749 return false;
750
751 return true;
752 }
753
754 /* Return true for the special block that we can't apply LCM optimization. */
755 static bool
756 invalid_opt_bb_p (basic_block cfg_bb)
757 {
758 edge e;
759 edge_iterator ei;
760
761 /* We don't do LCM optimizations on complex edges. */
762 FOR_EACH_EDGE (e, ei, cfg_bb->preds)
763 if (e->flags & EDGE_COMPLEX)
764 return true;
765
766 /* We only do LCM optimizations on blocks that are post dominated by
767 EXIT block, that is, we don't do LCM optimizations on infinite loop. */
768 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
769 if ((e->flags & EDGE_FAKE) || (e->flags & EDGE_ABNORMAL))
770 return true;
771
772 return false;
773 }
774
775 /* Get all predecessors of BB. */
776 static hash_set<basic_block>
777 get_all_predecessors (basic_block bb)
778 {
779 hash_set<basic_block> blocks;
780 auto_vec<basic_block> work_list;
781 hash_set<basic_block> visited_list;
782 work_list.safe_push (bb);
783
784 while (!work_list.is_empty ())
785 {
786 basic_block new_bb = work_list.pop ();
787 visited_list.add (new_bb);
788 edge e;
789 edge_iterator ei;
790 FOR_EACH_EDGE (e, ei, new_bb->preds)
791 {
792 if (!visited_list.contains (e->src))
793 work_list.safe_push (e->src);
794 blocks.add (e->src);
795 }
796 }
797 return blocks;
798 }
799
800 /* This flags indicates the minimum demand of the vl and vtype values by the
801 RVV instruction. For example, DEMAND_RATIO_P indicates that this RVV
802 instruction only needs the SEW/LMUL ratio to remain the same, and does not
803 require SEW and LMUL to be fixed.
804 Therefore, if the former RVV instruction needs DEMAND_RATIO_P and the latter
805 instruction needs DEMAND_SEW_LMUL_P and its SEW/LMUL is the same as that of
806 the former instruction, then we can make the minimum demand of the former
807 instruction strict to DEMAND_SEW_LMUL_P, and its required SEW and LMUL are
808 the SEW and LMUL of the latter instruction, and the vsetvl instruction
809 generated according to the new demand can also be used for the latter
810 instruction, so there is no need to insert a separate vsetvl instruction for
811 the latter instruction. */
812 enum demand_flags : unsigned
813 {
814 DEMAND_EMPTY_P = 0,
815 DEMAND_SEW_P = 1 << 0,
816 DEMAND_LMUL_P = 1 << 1,
817 DEMAND_RATIO_P = 1 << 2,
818 DEMAND_GE_SEW_P = 1 << 3,
819 DEMAND_TAIL_POLICY_P = 1 << 4,
820 DEMAND_MASK_POLICY_P = 1 << 5,
821 DEMAND_AVL_P = 1 << 6,
822 DEMAND_NON_ZERO_AVL_P = 1 << 7,
823 };
824
825 /* We split the demand information into three parts. They are sew and lmul
826 related (sew_lmul_demand_type), tail and mask policy related
827 (policy_demand_type) and avl related (avl_demand_type). Then we define three
828 interfaces available_p, compatible_p and merge. available_p is
829 used to determine whether the two vsetvl infos prev_info and next_info are
830 available or not. If prev_info is available for next_info, it means that the
831 RVV insn corresponding to next_info on the path from prev_info to next_info
832 can be used without inserting a separate vsetvl instruction. compatible_p
833 is used to determine whether prev_info is compatible with next_info, and if
834 so, merge can be used to merge the stricter demand information from
835 next_info into prev_info so that prev_info becomes available to next_info.
836 */
837
838 enum class sew_lmul_demand_type : unsigned
839 {
840 sew_lmul = demand_flags::DEMAND_SEW_P | demand_flags::DEMAND_LMUL_P,
841 ratio_only = demand_flags::DEMAND_RATIO_P,
842 sew_only = demand_flags::DEMAND_SEW_P,
843 ge_sew = demand_flags::DEMAND_GE_SEW_P,
844 ratio_and_ge_sew
845 = demand_flags::DEMAND_RATIO_P | demand_flags::DEMAND_GE_SEW_P,
846 };
847
848 enum class policy_demand_type : unsigned
849 {
850 tail_mask_policy
851 = demand_flags::DEMAND_TAIL_POLICY_P | demand_flags::DEMAND_MASK_POLICY_P,
852 tail_policy_only = demand_flags::DEMAND_TAIL_POLICY_P,
853 mask_policy_only = demand_flags::DEMAND_MASK_POLICY_P,
854 ignore_policy = demand_flags::DEMAND_EMPTY_P,
855 };
856
857 enum class avl_demand_type : unsigned
858 {
859 avl = demand_flags::DEMAND_AVL_P,
860 non_zero_avl = demand_flags::DEMAND_NON_ZERO_AVL_P,
861 ignore_avl = demand_flags::DEMAND_EMPTY_P,
862 };
863
864 /* Go through all uses of INSN looking for a single use of register REG.
865 Return true if we find
866 - Uses in a non-RVV insn
867 - More than one use in an RVV insn
868 - A single use in the VL operand of an RVV insn
869 and false otherwise.
870 A single use in the AVL operand does not count as use as we take care of
871 those separately in the pass. */
872
873 static bool
874 reg_used (insn_info *insn, rtx reg)
875 {
876 unsigned int regno = REGNO (reg);
877 const hash_set<use_info *> vl_uses = get_all_real_uses (insn, regno);
878 for (use_info *use : vl_uses)
879 {
880 gcc_assert (use->insn ()->is_real ());
881 rtx_insn *rinsn = use->insn ()->rtl ();
882 if (!has_vl_op (rinsn)
883 || count_regno_occurrences (rinsn, regno) != 1)
884 return true;
885
886 rtx avl = ::get_avl (rinsn);
887 if (!avl || !REG_P (avl) || regno != REGNO (avl))
888 return true;
889 }
890 return false;
891 }
892
893
894 class vsetvl_info
895 {
896 private:
897 insn_info *m_insn;
898 bb_info *m_bb;
899 rtx m_avl;
900 rtx m_vl;
901 set_info *m_avl_def;
902 uint8_t m_sew;
903 uint8_t m_max_sew;
904 vlmul_type m_vlmul;
905 uint8_t m_ratio;
906 bool m_ta;
907 bool m_ma;
908
909 sew_lmul_demand_type m_sew_lmul_demand;
910 policy_demand_type m_policy_demand;
911 avl_demand_type m_avl_demand;
912
913 enum class state_type
914 {
915 UNINITIALIZED,
916 VALID,
917 UNKNOWN,
918 EMPTY,
919 };
920 state_type m_state;
921
922 bool m_delete;
923 bool m_change_vtype_only;
924 insn_info *m_read_vl_insn;
925 bool m_vl_used_by_non_rvv_insn;
926
927 public:
928 vsetvl_info ()
929 : m_insn (nullptr), m_bb (nullptr), m_avl (NULL_RTX), m_vl (NULL_RTX),
930 m_avl_def (nullptr), m_sew (0), m_max_sew (0), m_vlmul (LMUL_RESERVED),
931 m_ratio (0), m_ta (false), m_ma (false),
932 m_sew_lmul_demand (sew_lmul_demand_type::sew_lmul),
933 m_policy_demand (policy_demand_type::tail_mask_policy),
934 m_avl_demand (avl_demand_type::avl), m_state (state_type::UNINITIALIZED),
935 m_delete (false), m_change_vtype_only (false), m_read_vl_insn (nullptr),
936 m_vl_used_by_non_rvv_insn (false)
937 {}
938
939 vsetvl_info (insn_info *insn) : vsetvl_info () { parse_insn (insn); }
940
941 vsetvl_info (rtx_insn *insn) : vsetvl_info () { parse_insn (insn); }
942
943 void set_avl (rtx avl) { m_avl = avl; }
944 void set_vl (rtx vl) { m_vl = vl; }
945 void set_avl_def (set_info *avl_def) { m_avl_def = avl_def; }
946 void set_sew (uint8_t sew) { m_sew = sew; }
947 void set_vlmul (vlmul_type vlmul) { m_vlmul = vlmul; }
948 void set_ratio (uint8_t ratio) { m_ratio = ratio; }
949 void set_ta (bool ta) { m_ta = ta; }
950 void set_ma (bool ma) { m_ma = ma; }
951 void set_delete () { m_delete = true; }
952 void set_bb (bb_info *bb) { m_bb = bb; }
953 void set_max_sew (uint8_t max_sew) { m_max_sew = max_sew; }
954 void set_change_vtype_only () { m_change_vtype_only = true; }
955 void set_read_vl_insn (insn_info *insn) { m_read_vl_insn = insn; }
956
957 rtx get_avl () const { return m_avl; }
958 rtx get_vl () const { return m_vl; }
959 set_info *get_avl_def () const { return m_avl_def; }
960 uint8_t get_sew () const { return m_sew; }
961 vlmul_type get_vlmul () const { return m_vlmul; }
962 uint8_t get_ratio () const { return m_ratio; }
963 bool get_ta () const { return m_ta; }
964 bool get_ma () const { return m_ma; }
965 insn_info *get_insn () const { return m_insn; }
966 bool delete_p () const { return m_delete; }
967 bb_info *get_bb () const { return m_bb; }
968 uint8_t get_max_sew () const { return m_max_sew; }
969 insn_info *get_read_vl_insn () const { return m_read_vl_insn; }
970 bool vl_used_by_non_rvv_insn_p () const { return m_vl_used_by_non_rvv_insn; }
971
972 bool has_imm_avl () const { return m_avl && CONST_INT_P (m_avl); }
973 bool has_vlmax_avl () const { return vlmax_avl_p (m_avl); }
974 bool has_nonvlmax_reg_avl () const
975 {
976 return m_avl && REG_P (m_avl) && !has_vlmax_avl ();
977 }
978 bool has_non_zero_avl () const
979 {
980 if (has_imm_avl ())
981 return INTVAL (m_avl) > 0;
982 return has_vlmax_avl ();
983 }
984 bool has_vl () const
985 {
986 /* The VL operand can only be either a NULL_RTX or a register. */
987 gcc_assert (!m_vl || REG_P (m_vl));
988 return m_vl != NULL_RTX;
989 }
990 bool has_same_ratio (const vsetvl_info &other) const
991 {
992 return get_ratio () == other.get_ratio ();
993 }
994
995 /* The block of INSN isn't always same as the block of the VSETVL_INFO,
996 meaning we may have 'get_insn ()->bb () != get_bb ()'.
997
998 E.g. BB 2 (Empty) ---> BB 3 (VALID, has rvv insn 1)
999
1000 BB 2 has empty VSETVL_INFO, wheras BB 3 has VSETVL_INFO that satisfies
1001 get_insn ()->bb () == get_bb (). In earliest fusion, we may fuse bb 3 and
1002 bb 2 so that the 'get_bb ()' of BB2 VSETVL_INFO will be BB2 wheras the
1003 'get_insn ()' of BB2 VSETVL INFO will be the rvv insn 1 (which is located
1004 at BB3). */
1005 bool insn_inside_bb_p () const { return get_insn ()->bb () == get_bb (); }
1006 void update_avl (const vsetvl_info &other)
1007 {
1008 m_avl = other.get_avl ();
1009 m_vl = other.get_vl ();
1010 m_avl_def = other.get_avl_def ();
1011 }
1012
1013 bool uninit_p () const { return m_state == state_type::UNINITIALIZED; }
1014 bool valid_p () const { return m_state == state_type::VALID; }
1015 bool unknown_p () const { return m_state == state_type::UNKNOWN; }
1016 bool empty_p () const { return m_state == state_type::EMPTY; }
1017 bool change_vtype_only_p () const { return m_change_vtype_only
1018 && !TARGET_XTHEADVECTOR; }
1019
1020 void set_valid () { m_state = state_type::VALID; }
1021 void set_unknown () { m_state = state_type::UNKNOWN; }
1022 void set_empty () { m_state = state_type::EMPTY; }
1023
1024 void set_sew_lmul_demand (sew_lmul_demand_type demand)
1025 {
1026 m_sew_lmul_demand = demand;
1027 }
1028 void set_policy_demand (policy_demand_type demand)
1029 {
1030 m_policy_demand = demand;
1031 }
1032 void set_avl_demand (avl_demand_type demand) { m_avl_demand = demand; }
1033
1034 sew_lmul_demand_type get_sew_lmul_demand () const
1035 {
1036 return m_sew_lmul_demand;
1037 }
1038 policy_demand_type get_policy_demand () const { return m_policy_demand; }
1039 avl_demand_type get_avl_demand () const { return m_avl_demand; }
1040
1041 void normalize_demand (unsigned demand_flags)
1042 {
1043 switch (demand_flags
1044 & (DEMAND_SEW_P | DEMAND_LMUL_P | DEMAND_RATIO_P | DEMAND_GE_SEW_P))
1045 {
1046 case (unsigned) sew_lmul_demand_type::sew_lmul:
1047 m_sew_lmul_demand = sew_lmul_demand_type::sew_lmul;
1048 break;
1049 case (unsigned) sew_lmul_demand_type::ratio_only:
1050 m_sew_lmul_demand = sew_lmul_demand_type::ratio_only;
1051 break;
1052 case (unsigned) sew_lmul_demand_type::sew_only:
1053 m_sew_lmul_demand = sew_lmul_demand_type::sew_only;
1054 break;
1055 case (unsigned) sew_lmul_demand_type::ge_sew:
1056 m_sew_lmul_demand = sew_lmul_demand_type::ge_sew;
1057 break;
1058 case (unsigned) sew_lmul_demand_type::ratio_and_ge_sew:
1059 m_sew_lmul_demand = sew_lmul_demand_type::ratio_and_ge_sew;
1060 break;
1061 default:
1062 gcc_unreachable ();
1063 }
1064
1065 switch (demand_flags & (DEMAND_TAIL_POLICY_P | DEMAND_MASK_POLICY_P))
1066 {
1067 case (unsigned) policy_demand_type::tail_mask_policy:
1068 m_policy_demand = policy_demand_type::tail_mask_policy;
1069 break;
1070 case (unsigned) policy_demand_type::tail_policy_only:
1071 m_policy_demand = policy_demand_type::tail_policy_only;
1072 break;
1073 case (unsigned) policy_demand_type::mask_policy_only:
1074 m_policy_demand = policy_demand_type::mask_policy_only;
1075 break;
1076 case (unsigned) policy_demand_type::ignore_policy:
1077 m_policy_demand = policy_demand_type::ignore_policy;
1078 break;
1079 default:
1080 gcc_unreachable ();
1081 }
1082
1083 switch (demand_flags & (DEMAND_AVL_P | DEMAND_NON_ZERO_AVL_P))
1084 {
1085 case (unsigned) avl_demand_type::avl:
1086 m_avl_demand = avl_demand_type::avl;
1087 break;
1088 case (unsigned) avl_demand_type::non_zero_avl:
1089 m_avl_demand = avl_demand_type::non_zero_avl;
1090 break;
1091 case (unsigned) avl_demand_type::ignore_avl:
1092 m_avl_demand = avl_demand_type::ignore_avl;
1093 break;
1094 default:
1095 gcc_unreachable ();
1096 }
1097 }
1098
1099 void parse_insn (rtx_insn *rinsn)
1100 {
1101 if (!NONDEBUG_INSN_P (rinsn))
1102 return;
1103 if (optimize == 0 && !has_vtype_op (rinsn))
1104 return;
1105 gcc_assert (!vsetvl_discard_result_insn_p (rinsn));
1106 set_valid ();
1107 extract_insn_cached (rinsn);
1108 m_avl = ::get_avl (rinsn);
1109 if (has_vlmax_avl () || vsetvl_insn_p (rinsn))
1110 m_vl = ::get_vl (rinsn);
1111 m_sew = ::get_sew (rinsn);
1112 m_vlmul = ::get_vlmul (rinsn);
1113 m_ta = tail_agnostic_p (rinsn);
1114 m_ma = mask_agnostic_p (rinsn);
1115 }
1116
1117 void parse_insn (insn_info *insn)
1118 {
1119 /* The VL dest of the insn */
1120 rtx dest_vl = NULL_RTX;
1121
1122 m_insn = insn;
1123 m_bb = insn->bb ();
1124 /* Return if it is debug insn for the consistency with optimize == 0. */
1125 if (insn->is_debug_insn ())
1126 return;
1127
1128 /* We set it as unknown since we don't what will happen in CALL or ASM. */
1129 if (insn->is_call () || insn->is_asm ())
1130 {
1131 set_unknown ();
1132 return;
1133 }
1134
1135 /* If this is something that updates VL/VTYPE that we don't know about, set
1136 the state to unknown. */
1137 if (!vector_config_insn_p (insn->rtl ()) && !has_vtype_op (insn->rtl ())
1138 && (find_access (insn->defs (), VL_REGNUM)
1139 || find_access (insn->defs (), VTYPE_REGNUM)))
1140 {
1141 set_unknown ();
1142 return;
1143 }
1144
1145 if (!vector_config_insn_p (insn->rtl ()) && !has_vtype_op (insn->rtl ()))
1146 /* uninitialized */
1147 return;
1148
1149 set_valid ();
1150
1151 m_avl = ::get_avl (insn->rtl ());
1152 if (m_avl)
1153 {
1154 if (vsetvl_insn_p (insn->rtl ()) || has_vlmax_avl ())
1155 {
1156 m_vl = ::get_vl (insn->rtl ());
1157 dest_vl = m_vl;
1158 }
1159
1160 if (has_nonvlmax_reg_avl ())
1161 m_avl_def = find_access (insn->uses (), REGNO (m_avl))->def ();
1162 }
1163
1164 m_sew = ::get_sew (insn->rtl ());
1165 m_vlmul = ::get_vlmul (insn->rtl ());
1166 m_ratio = get_attr_ratio (insn->rtl ());
1167 /* when get_attr_ratio is invalid, this kind of instructions
1168 doesn't care about ratio. However, we still need this value
1169 in demand info backward analysis. */
1170 if (m_ratio == INVALID_ATTRIBUTE)
1171 m_ratio = calculate_ratio (m_sew, m_vlmul);
1172 m_ta = tail_agnostic_p (insn->rtl ());
1173 m_ma = mask_agnostic_p (insn->rtl ());
1174
1175 /* If merge operand is undef value, we prefer agnostic. */
1176 int merge_op_idx = get_attr_merge_op_idx (insn->rtl ());
1177 if (merge_op_idx != INVALID_ATTRIBUTE
1178 && satisfies_constraint_vu (recog_data.operand[merge_op_idx]))
1179 {
1180 m_ta = true;
1181 m_ma = true;
1182 }
1183
1184 /* Determine the demand info of the RVV insn. */
1185 m_max_sew = get_max_int_sew ();
1186 unsigned dflags = 0;
1187 if (vector_config_insn_p (insn->rtl ()))
1188 {
1189 dflags |= demand_flags::DEMAND_AVL_P;
1190 dflags |= demand_flags::DEMAND_RATIO_P;
1191 }
1192 else
1193 {
1194 if (has_vl_op (insn->rtl ()))
1195 {
1196 if (scalar_move_insn_p (insn->rtl ()))
1197 {
1198 /* If the avl for vmv.s.x comes from the vsetvl instruction, we
1199 don't know if the avl is non-zero, so it is set to
1200 DEMAND_AVL_P for now. it may be corrected to
1201 DEMAND_NON_ZERO_AVL_P later when more information is
1202 available.
1203 */
1204 if (has_non_zero_avl ())
1205 dflags |= demand_flags::DEMAND_NON_ZERO_AVL_P;
1206 else
1207 dflags |= demand_flags::DEMAND_AVL_P;
1208 }
1209 else
1210 dflags |= demand_flags::DEMAND_AVL_P;
1211 }
1212
1213 if (get_attr_ratio (insn->rtl ()) != INVALID_ATTRIBUTE)
1214 dflags |= demand_flags::DEMAND_RATIO_P;
1215 else
1216 {
1217 if (scalar_move_insn_p (insn->rtl ()) && m_ta)
1218 {
1219 dflags |= demand_flags::DEMAND_GE_SEW_P;
1220 m_max_sew = get_attr_type (insn->rtl ()) == TYPE_VFMOVFV
1221 ? get_max_float_sew ()
1222 : get_max_int_sew ();
1223 }
1224 else
1225 dflags |= demand_flags::DEMAND_SEW_P;
1226
1227 if (!ignore_vlmul_insn_p (insn->rtl ()))
1228 dflags |= demand_flags::DEMAND_LMUL_P;
1229 }
1230
1231 if (!m_ta)
1232 dflags |= demand_flags::DEMAND_TAIL_POLICY_P;
1233 if (!m_ma)
1234 dflags |= demand_flags::DEMAND_MASK_POLICY_P;
1235 }
1236
1237 normalize_demand (dflags);
1238
1239 /* Optimize AVL from the vsetvl instruction. */
1240 insn_info *def_insn = extract_single_source (get_avl_def ());
1241 if (def_insn && vsetvl_insn_p (def_insn->rtl ()))
1242 {
1243 vsetvl_info def_info = vsetvl_info (def_insn);
1244 if ((scalar_move_insn_p (insn->rtl ())
1245 || def_info.get_ratio () == get_ratio ())
1246 && (def_info.has_vlmax_avl () || def_info.has_imm_avl ()))
1247 {
1248 update_avl (def_info);
1249 if (scalar_move_insn_p (insn->rtl ()) && has_non_zero_avl ())
1250 m_avl_demand = avl_demand_type::non_zero_avl;
1251 }
1252 }
1253
1254 /* Determine if dest operand(vl) has been used by non-RVV instructions. */
1255 if (dest_vl)
1256 m_vl_used_by_non_rvv_insn = reg_used (get_insn (), dest_vl);
1257
1258 /* Collect the read vl insn for the fault-only-first rvv loads. */
1259 if (fault_first_load_p (insn->rtl ()))
1260 {
1261 for (insn_info *i = insn->next_nondebug_insn ();
1262 i && i->bb () == insn->bb (); i = i->next_nondebug_insn ())
1263 {
1264 if (find_access (i->defs (), VL_REGNUM))
1265 break;
1266 if (i->rtl () && read_vl_insn_p (i->rtl ()))
1267 {
1268 m_read_vl_insn = i;
1269 break;
1270 }
1271 }
1272 /* If no csrr found but this is a _set_vl style fault-only-first
1273 load, use the insn itself as the VL source.
1274 If we have two identical vector configs that just differ in
1275 AVL and the AVL is just "modified" by a read_vl we
1276 can consider them equal and elide the second one. */
1277 if (!m_read_vl_insn && get_fof_set_vl_reg (insn->rtl ()))
1278 m_read_vl_insn = insn;
1279 }
1280 }
1281
1282 /* Returns the corresponding vsetvl rtx pat. */
1283 rtx get_vsetvl_pat (bool ignore_vl = false) const
1284 {
1285 rtx avl = get_avl ();
1286 /* if optimization == 0 and the instruction is vmv.x.s/vfmv.f.s,
1287 set the value of avl to (const_int 0) so that VSETVL PASS will
1288 insert vsetvl correctly.*/
1289 if (!get_avl ())
1290 avl = GEN_INT (0);
1291 rtx sew = gen_int_mode (get_sew (), Pmode);
1292 rtx vlmul = gen_int_mode (get_vlmul (), Pmode);
1293 rtx ta = gen_int_mode (get_ta (), Pmode);
1294 rtx ma = gen_int_mode (get_ma (), Pmode);
1295
1296 if (change_vtype_only_p ())
1297 return gen_vsetvl_vtype_change_only (sew, vlmul, ta, ma);
1298 else if (has_vl () && !ignore_vl)
1299 return gen_vsetvl (Pmode, get_vl (), avl, sew, vlmul, ta, ma);
1300 else
1301 return gen_vsetvl_discard_result (Pmode, avl, sew, vlmul, ta, ma);
1302 }
1303
1304 /* Return true that the non-AVL operands of THIS will be modified
1305 if we fuse the VL modification from OTHER into THIS. */
1306 bool vl_modify_non_avl_op_p (const vsetvl_info &other) const
1307 {
1308 /* We don't need to worry about any operands from THIS be
1309 modified by OTHER vsetvl since we OTHER vsetvl doesn't
1310 modify any operand. */
1311 if (!other.has_vl ())
1312 return false;
1313
1314 /* THIS VL operand always preempt OTHER VL operand. */
1315 if (this->has_vl ())
1316 return false;
1317
1318 /* If THIS has non IMM AVL and THIS is AVL compatible with
1319 OTHER, the AVL value of THIS is same as VL value of OTHER. */
1320 if (!this->has_imm_avl ())
1321 return false;
1322 return find_access (this->get_insn ()->uses (), REGNO (other.get_vl ()));
1323 }
1324
1325 bool operator== (const vsetvl_info &other) const
1326 {
1327 gcc_assert (!uninit_p () && !other.uninit_p ()
1328 && "Uninitialization should not happen");
1329
1330 if (empty_p ())
1331 return other.empty_p ();
1332 if (unknown_p ())
1333 return other.unknown_p ();
1334
1335 return get_insn () == other.get_insn () && get_bb () == other.get_bb ()
1336 && get_avl () == other.get_avl () && get_vl () == other.get_vl ()
1337 && get_avl_def () == other.get_avl_def ()
1338 && get_sew () == other.get_sew ()
1339 && get_vlmul () == other.get_vlmul () && get_ta () == other.get_ta ()
1340 && get_ma () == other.get_ma ()
1341 && get_avl_demand () == other.get_avl_demand ()
1342 && get_sew_lmul_demand () == other.get_sew_lmul_demand ()
1343 && get_policy_demand () == other.get_policy_demand ();
1344 }
1345
1346 void dump (FILE *file, const char *indent = "") const
1347 {
1348 if (uninit_p ())
1349 {
1350 fprintf (file, "UNINITIALIZED.\n");
1351 return;
1352 }
1353 else if (unknown_p ())
1354 {
1355 fprintf (file, "UNKNOWN.\n");
1356 return;
1357 }
1358 else if (empty_p ())
1359 {
1360 fprintf (file, "EMPTY.\n");
1361 return;
1362 }
1363 else if (valid_p ())
1364 fprintf (file, "VALID (insn %u, bb %u)%s\n", get_insn ()->uid (),
1365 get_bb ()->index (), delete_p () ? " (deleted)" : "");
1366 else
1367 gcc_unreachable ();
1368
1369 fprintf (file, "%sDemand fields:", indent);
1370 if (m_sew_lmul_demand == sew_lmul_demand_type::sew_lmul)
1371 fprintf (file, " demand_sew_lmul");
1372 else if (m_sew_lmul_demand == sew_lmul_demand_type::ratio_only)
1373 fprintf (file, " demand_ratio_only");
1374 else if (m_sew_lmul_demand == sew_lmul_demand_type::sew_only)
1375 fprintf (file, " demand_sew_only");
1376 else if (m_sew_lmul_demand == sew_lmul_demand_type::ge_sew)
1377 fprintf (file, " demand_ge_sew");
1378 else if (m_sew_lmul_demand == sew_lmul_demand_type::ratio_and_ge_sew)
1379 fprintf (file, " demand_ratio_and_ge_sew");
1380
1381 if (m_policy_demand == policy_demand_type::tail_mask_policy)
1382 fprintf (file, " demand_tail_mask_policy");
1383 else if (m_policy_demand == policy_demand_type::tail_policy_only)
1384 fprintf (file, " demand_tail_policy_only");
1385 else if (m_policy_demand == policy_demand_type::mask_policy_only)
1386 fprintf (file, " demand_mask_policy_only");
1387
1388 if (m_avl_demand == avl_demand_type::avl)
1389 fprintf (file, " demand_avl");
1390 else if (m_avl_demand == avl_demand_type::non_zero_avl)
1391 fprintf (file, " demand_non_zero_avl");
1392 fprintf (file, "\n");
1393
1394 fprintf (file, "%sSEW=%d, ", indent, get_sew ());
1395 fprintf (file, "VLMUL=%s, ", vlmul_to_str (get_vlmul ()));
1396 fprintf (file, "RATIO=%d, ", get_ratio ());
1397 fprintf (file, "MAX_SEW=%d\n", get_max_sew ());
1398
1399 fprintf (file, "%sTAIL_POLICY=%s, ", indent, policy_to_str (get_ta ()));
1400 fprintf (file, "MASK_POLICY=%s\n", policy_to_str (get_ma ()));
1401
1402 fprintf (file, "%sAVL=", indent);
1403 print_rtl_single (file, get_avl ());
1404 fprintf (file, "%sVL=", indent);
1405 print_rtl_single (file, get_vl ());
1406 if (change_vtype_only_p ())
1407 fprintf (file, "%schange vtype only\n", indent);
1408 if (get_read_vl_insn ())
1409 fprintf (file, "%sread_vl_insn: insn %u\n", indent,
1410 get_read_vl_insn ()->uid ());
1411 if (vl_used_by_non_rvv_insn_p ())
1412 fprintf (file, "%suse_by_non_rvv_insn=true\n", indent);
1413 }
1414 };
1415
1416 class vsetvl_block_info
1417 {
1418 public:
1419 /* The static execute probability of the demand info. */
1420 profile_probability probability;
1421
1422 auto_vec<vsetvl_info> local_infos;
1423 vsetvl_info global_info;
1424 bb_info *bb;
1425
1426 vsetvl_block_info () : bb (nullptr)
1427 {
1428 local_infos.safe_grow_cleared (0);
1429 global_info.set_empty ();
1430 }
1431 vsetvl_block_info (const vsetvl_block_info &other)
1432 : probability (other.probability), local_infos (other.local_infos.copy ()),
1433 global_info (other.global_info), bb (other.bb)
1434 {}
1435
1436 vsetvl_info &get_entry_info ()
1437 {
1438 gcc_assert (!empty_p ());
1439 return local_infos.is_empty () ? global_info : local_infos[0];
1440 }
1441 vsetvl_info &get_exit_info ()
1442 {
1443 gcc_assert (!empty_p ());
1444 return local_infos.is_empty () ? global_info
1445 : local_infos[local_infos.length () - 1];
1446 }
1447 const vsetvl_info &get_entry_info () const
1448 {
1449 gcc_assert (!empty_p ());
1450 return local_infos.is_empty () ? global_info : local_infos[0];
1451 }
1452 const vsetvl_info &get_exit_info () const
1453 {
1454 gcc_assert (!empty_p ());
1455 return local_infos.is_empty () ? global_info
1456 : local_infos[local_infos.length () - 1];
1457 }
1458
1459 bool empty_p () const { return local_infos.is_empty () && !has_info (); }
1460 bool has_info () const { return !global_info.empty_p (); }
1461 void set_info (const vsetvl_info &info)
1462 {
1463 gcc_assert (local_infos.is_empty ());
1464 global_info = info;
1465 global_info.set_bb (bb);
1466 }
1467 void set_empty_info () { global_info.set_empty (); }
1468 };
1469
1470 /* Same as REG_USED () but looks for a single use in an RVV insn's AVL
1471 operand. */
1472 static bool
1473 reg_single_use_in_avl (insn_info *insn, rtx reg)
1474 {
1475 if (!reg)
1476 return false;
1477 unsigned int regno = REGNO (reg);
1478 const hash_set<use_info *> vl_uses = get_all_real_uses (insn, regno);
1479 for (use_info *use : vl_uses)
1480 {
1481 gcc_assert (use->insn ()->is_real ());
1482 rtx_insn *rinsn = use->insn ()->rtl ();
1483 if (!has_vl_op (rinsn)
1484 || count_regno_occurrences (rinsn, regno) != 1)
1485 return false;
1486
1487 vsetvl_info info = vsetvl_info (use->insn ());
1488
1489 if (!info.has_nonvlmax_reg_avl ())
1490 return false;
1491
1492 rtx avl = info.get_avl ();
1493 if (avl && REG_P (avl) && regno == REGNO (avl))
1494 return true;
1495 }
1496 return false;
1497 }
1498
1499 /* Demand system is the RVV-based VSETVL info analysis tools wrapper.
1500 It defines compatible rules for SEW/LMUL, POLICY and AVL.
1501 Also, it provides 3 interfaces available_p, compatible_p and
1502 merge for the VSETVL PASS analysis and optimization.
1503
1504 - available_p: Determine whether the next info can get the
1505 available VSETVL status from previous info.
1506 e.g. bb 2 (demand SEW = 32, LMUL = M2) -> bb 3 (demand RATIO = 16).
1507 Since bb 2 demand info (SEW/LMUL = 32/2 = 16) satisfies the bb 3
1508 demand, the VSETVL instruction in bb 3 can be elided.
1509 available_p (previous, next) is true in such situation.
1510 - compatible_p: Determine whether prev_info is compatible with next_info
1511 so that we can have a new merged info that is available to both of them.
1512 - merge: Merge the stricter demand information from
1513 next_info into prev_info so that prev_info becomes available to
1514 next_info. */
1515 class demand_system
1516 {
1517 private:
1518 /* predictors. */
1519
1520 inline bool always_true (const vsetvl_info &prev ATTRIBUTE_UNUSED,
1521 const vsetvl_info &next ATTRIBUTE_UNUSED)
1522 {
1523 return true;
1524 }
1525 inline bool always_false (const vsetvl_info &prev ATTRIBUTE_UNUSED,
1526 const vsetvl_info &next ATTRIBUTE_UNUSED)
1527 {
1528 return false;
1529 }
1530
1531 /* predictors for sew and lmul */
1532
1533 inline bool lmul_eq_p (const vsetvl_info &prev, const vsetvl_info &next)
1534 {
1535 return prev.get_vlmul () == next.get_vlmul ();
1536 }
1537 inline bool sew_eq_p (const vsetvl_info &prev, const vsetvl_info &next)
1538 {
1539 return prev.get_sew () == next.get_sew ();
1540 }
1541 inline bool sew_lmul_eq_p (const vsetvl_info &prev, const vsetvl_info &next)
1542 {
1543 return lmul_eq_p (prev, next) && sew_eq_p (prev, next);
1544 }
1545 inline bool sew_ge_p (const vsetvl_info &prev, const vsetvl_info &next)
1546 {
1547 return prev.get_sew () == next.get_sew ()
1548 || (next.get_ta () && prev.get_sew () > next.get_sew ());
1549 }
1550 inline bool sew_le_p (const vsetvl_info &prev, const vsetvl_info &next)
1551 {
1552 return prev.get_sew () == next.get_sew ()
1553 || (prev.get_ta () && prev.get_sew () < next.get_sew ());
1554 }
1555 inline bool prev_sew_le_next_max_sew_p (const vsetvl_info &prev,
1556 const vsetvl_info &next)
1557 {
1558 return prev.get_sew () <= next.get_max_sew ();
1559 }
1560 inline bool next_sew_le_prev_max_sew_p (const vsetvl_info &prev,
1561 const vsetvl_info &next)
1562 {
1563 return next.get_sew () <= prev.get_max_sew ();
1564 }
1565 inline bool max_sew_overlap_p (const vsetvl_info &prev,
1566 const vsetvl_info &next)
1567 {
1568 return !(prev.get_sew () > next.get_max_sew ()
1569 || next.get_sew () > prev.get_max_sew ());
1570 }
1571 inline bool ratio_eq_p (const vsetvl_info &prev, const vsetvl_info &next)
1572 {
1573 return prev.has_same_ratio (next);
1574 }
1575 inline bool prev_ratio_valid_for_next_sew_p (const vsetvl_info &prev,
1576 const vsetvl_info &next)
1577 {
1578 return prev.get_ratio () >= (next.get_sew () / MAX_LMUL);
1579 }
1580 inline bool next_ratio_valid_for_prev_sew_p (const vsetvl_info &prev,
1581 const vsetvl_info &next)
1582 {
1583 return next.get_ratio () >= (prev.get_sew () / MAX_LMUL);
1584 }
1585 inline bool sew_ge_and_ratio_eq_p (const vsetvl_info &prev,
1586 const vsetvl_info &next)
1587 {
1588 return sew_ge_p (prev, next) && ratio_eq_p (prev, next);
1589 }
1590 inline bool sew_ge_and_prev_sew_le_next_max_sew_p (const vsetvl_info &prev,
1591 const vsetvl_info &next)
1592 {
1593 return sew_ge_p (prev, next) && prev_sew_le_next_max_sew_p (prev, next);
1594 }
1595 inline bool
1596 sew_ge_and_prev_sew_le_next_max_sew_and_next_ratio_valid_for_prev_sew_p (
1597 const vsetvl_info &prev, const vsetvl_info &next)
1598 {
1599 return sew_ge_p (prev, next) && prev_sew_le_next_max_sew_p (prev, next)
1600 && next_ratio_valid_for_prev_sew_p (prev, next);
1601 }
1602 inline bool
1603 sew_ge_and_prev_sew_le_next_max_sew_and_ratio_eq_p (
1604 const vsetvl_info &prev, const vsetvl_info &next)
1605 {
1606 return sew_ge_p (prev, next) && prev_sew_le_next_max_sew_p (prev, next)
1607 && ratio_eq_p (prev, next);
1608 }
1609 inline bool sew_le_and_next_sew_le_prev_max_sew_p (const vsetvl_info &prev,
1610 const vsetvl_info &next)
1611 {
1612 return sew_le_p (prev, next) && next_sew_le_prev_max_sew_p (prev, next);
1613 }
1614 inline bool
1615 max_sew_overlap_and_next_ratio_valid_for_prev_sew_p (const vsetvl_info &prev,
1616 const vsetvl_info &next)
1617 {
1618 if (next_ratio_valid_for_prev_sew_p (prev, next)
1619 && max_sew_overlap_p (prev, next))
1620 {
1621 if (next.get_sew () < prev.get_sew ()
1622 && (!next.get_ta () || !next.get_ma ()))
1623 return false;
1624 return true;
1625 }
1626 return false;
1627 }
1628 inline bool
1629 sew_le_and_next_sew_le_prev_max_sew_and_ratio_eq_p (const vsetvl_info &prev,
1630 const vsetvl_info &next)
1631 {
1632 return sew_le_p (prev, next) && ratio_eq_p (prev, next)
1633 && next_sew_le_prev_max_sew_p (prev, next);
1634 }
1635 inline bool
1636 max_sew_overlap_and_prev_ratio_valid_for_next_sew_p (const vsetvl_info &prev,
1637 const vsetvl_info &next)
1638 {
1639 return prev_ratio_valid_for_next_sew_p (prev, next)
1640 && max_sew_overlap_p (prev, next);
1641 }
1642 inline bool
1643 sew_le_and_next_sew_le_prev_max_sew_and_prev_ratio_valid_for_next_sew_p (
1644 const vsetvl_info &prev, const vsetvl_info &next)
1645 {
1646 return sew_le_p (prev, next) && prev_ratio_valid_for_next_sew_p (prev, next)
1647 && next_sew_le_prev_max_sew_p (prev, next);
1648 }
1649 inline bool max_sew_overlap_and_ratio_eq_p (const vsetvl_info &prev,
1650 const vsetvl_info &next)
1651 {
1652 return ratio_eq_p (prev, next) && max_sew_overlap_p (prev, next);
1653 }
1654
1655 /* predictors for tail and mask policy */
1656
1657 inline bool tail_policy_eq_p (const vsetvl_info &prev,
1658 const vsetvl_info &next)
1659 {
1660 return prev.get_ta () == next.get_ta ();
1661 }
1662 inline bool mask_policy_eq_p (const vsetvl_info &prev,
1663 const vsetvl_info &next)
1664 {
1665 return prev.get_ma () == next.get_ma ();
1666 }
1667 inline bool tail_mask_policy_eq_p (const vsetvl_info &prev,
1668 const vsetvl_info &next)
1669 {
1670 return tail_policy_eq_p (prev, next) && mask_policy_eq_p (prev, next);
1671 }
1672
1673 /* predictors for avl */
1674
1675 inline bool modify_or_use_vl_p (insn_info *i, const vsetvl_info &info)
1676 {
1677 if (info.has_vl ())
1678 {
1679 if (find_access (i->defs (), REGNO (info.get_vl ())))
1680 return true;
1681 if (find_access (i->uses (), REGNO (info.get_vl ())))
1682 {
1683 resource_info resource = full_register (REGNO (info.get_vl ()));
1684 def_lookup dl1 = crtl->ssa->find_def (resource, i);
1685 def_lookup dl2 = crtl->ssa->find_def (resource, info.get_insn ());
1686 if (dl1.matching_set () || dl2.matching_set ())
1687 return true;
1688 /* If their VLs are coming from same def, we still want to fuse
1689 their VSETVL demand info to gain better performance. */
1690 return dl1.prev_def (i) != dl2.prev_def (i);
1691 }
1692 }
1693 return false;
1694 }
1695 inline bool modify_avl_p (insn_info *i, const vsetvl_info &info)
1696 {
1697 return info.has_nonvlmax_reg_avl ()
1698 && find_access (i->defs (), REGNO (info.get_avl ()));
1699 }
1700
1701 inline bool modify_reg_between_p (insn_info *prev_insn, insn_info *curr_insn,
1702 unsigned regno)
1703 {
1704 gcc_assert (prev_insn->compare_with (curr_insn) < 0);
1705 for (insn_info *i = curr_insn->prev_nondebug_insn (); i != prev_insn;
1706 i = i->prev_nondebug_insn ())
1707 {
1708 // no def of regno
1709 if (find_access (i->defs (), regno))
1710 return true;
1711 }
1712 return false;
1713 }
1714
1715 inline bool reg_avl_equal_p (const vsetvl_info &prev, const vsetvl_info &next)
1716 {
1717 if (!prev.has_nonvlmax_reg_avl () || !next.has_nonvlmax_reg_avl ())
1718 return false;
1719
1720 if (same_equiv_note_p (prev.get_avl_def (), next.get_avl_def ()))
1721 return true;
1722
1723 if (REGNO (prev.get_avl ()) != REGNO (next.get_avl ()))
1724 return false;
1725
1726 insn_info *prev_insn = prev.get_insn ();
1727 if (prev.get_bb () != prev_insn->bb ())
1728 prev_insn = prev.get_bb ()->end_insn ();
1729
1730 insn_info *next_insn = next.get_insn ();
1731 if (next.get_bb () != next_insn->bb ())
1732 next_insn = next.get_bb ()->end_insn ();
1733
1734 return avl_vl_unmodified_between_p (prev_insn, next_insn, next, false);
1735 }
1736
1737 inline bool avl_equal_p (const vsetvl_info &prev, const vsetvl_info &next)
1738 {
1739 gcc_assert (prev.valid_p () && next.valid_p ());
1740
1741 if (next.has_vl () && next.vl_used_by_non_rvv_insn_p ())
1742 return false;
1743
1744 if (vector_config_insn_p (prev.get_insn ()->rtl ()) && next.get_avl_def ()
1745 && next.get_avl_def ()->insn () == prev.get_insn ())
1746 return true;
1747
1748 if (prev.get_read_vl_insn ())
1749 {
1750 if (!next.has_nonvlmax_reg_avl () || !next.get_avl_def ())
1751 return false;
1752 insn_info *avl_def_insn = extract_single_source (next.get_avl_def ());
1753 return avl_def_insn == prev.get_read_vl_insn ();
1754 }
1755
1756 if (prev == next && prev.has_nonvlmax_reg_avl ())
1757 {
1758 insn_info *insn = prev.get_insn ();
1759 bb_info *bb = insn->bb ();
1760 for (insn_info *i = insn; real_insn_and_same_bb_p (i, bb);
1761 i = i->next_nondebug_insn ())
1762 if (find_access (i->defs (), REGNO (prev.get_avl ())))
1763 return false;
1764 }
1765
1766 if (prev.has_vlmax_avl () && next.has_vlmax_avl ())
1767 return true;
1768 else if (prev.has_imm_avl () && next.has_imm_avl ())
1769 return INTVAL (prev.get_avl ()) == INTVAL (next.get_avl ());
1770 else if (prev.has_vl () && next.has_nonvlmax_reg_avl ()
1771 && REGNO (prev.get_vl ()) == REGNO (next.get_avl ()))
1772 {
1773 insn_info *prev_insn = prev.insn_inside_bb_p ()
1774 ? prev.get_insn ()
1775 : prev.get_bb ()->end_insn ();
1776
1777 insn_info *next_insn = next.insn_inside_bb_p ()
1778 ? next.get_insn ()
1779 : next.get_bb ()->end_insn ();
1780 return avl_vl_unmodified_between_p (prev_insn, next_insn, next, false);
1781 }
1782 else if (prev.has_nonvlmax_reg_avl () && next.has_nonvlmax_reg_avl ())
1783 return reg_avl_equal_p (prev, next);
1784
1785 return false;
1786 }
1787 inline bool avl_equal_or_prev_avl_non_zero_p (const vsetvl_info &prev,
1788 const vsetvl_info &next)
1789 {
1790 return avl_equal_p (prev, next) || prev.has_non_zero_avl ();
1791 }
1792
1793 inline bool can_use_next_avl_p (const vsetvl_info &prev,
1794 const vsetvl_info &next)
1795 {
1796 /* Forbid the AVL/VL propagation if VL of NEXT is used
1797 by non-RVV instructions. This is because:
1798
1799 bb 2:
1800 PREV: scalar move (no AVL)
1801 bb 3:
1802 NEXT: vsetvl a5(VL), a4(AVL) ...
1803 branch a5,zero
1804
1805 Since user vsetvl instruction is no side effect instruction
1806 which should be placed in the correct and optimal location
1807 of the program by the previous PASS, it is unreasonable that
1808 VSETVL PASS tries to move it to another places if it used by
1809 non-RVV instructions.
1810
1811 Note: We only forbid the cases that VL is used by the following
1812 non-RVV instructions which will cause issues. We don't forbid
1813 other cases since it won't cause correctness issues and we still
1814 more demand info are fused backward. The later LCM algorithm
1815 should know the optimal location of the vsetvl. */
1816 if (next.has_vl () && next.vl_used_by_non_rvv_insn_p ())
1817 return false;
1818
1819 if (!next.has_nonvlmax_reg_avl () && !next.has_vl ())
1820 return true;
1821
1822 insn_info *prev_insn = prev.get_insn ();
1823 if (prev.get_bb () != prev_insn->bb ())
1824 prev_insn = prev.get_bb ()->end_insn ();
1825
1826 insn_info *next_insn = next.get_insn ();
1827 if (next.get_bb () != next_insn->bb ())
1828 next_insn = next.get_bb ()->end_insn ();
1829
1830 return avl_vl_unmodified_between_p (prev_insn, next_insn, next);
1831 }
1832
1833 inline bool avl_equal_or_next_avl_non_zero_and_can_use_next_avl_p (
1834 const vsetvl_info &prev, const vsetvl_info &next)
1835 {
1836 return avl_equal_p (prev, next)
1837 || (next.has_non_zero_avl () && can_use_next_avl_p (prev, next));
1838 }
1839
1840 /* modifiers */
1841
1842 inline void nop (const vsetvl_info &prev ATTRIBUTE_UNUSED,
1843 const vsetvl_info &next ATTRIBUTE_UNUSED)
1844 {}
1845
1846 /* modifiers for sew and lmul */
1847
1848 inline void use_min_of_max_sew (vsetvl_info &prev, const vsetvl_info &next)
1849 {
1850 prev.set_max_sew (MIN (prev.get_max_sew (), next.get_max_sew ()));
1851 }
1852 inline void use_next_sew (vsetvl_info &prev, const vsetvl_info &next)
1853 {
1854 prev.set_sew (next.get_sew ());
1855 use_min_of_max_sew (prev, next);
1856 }
1857 inline void use_max_sew (vsetvl_info &prev, const vsetvl_info &next)
1858 {
1859 bool prev_sew_larger = prev.get_sew () >= next.get_sew ();
1860 const vsetvl_info from = prev_sew_larger ? prev : next;
1861 prev.set_sew (from.get_sew ());
1862 prev.set_vlmul (from.get_vlmul ());
1863 prev.set_ratio (from.get_ratio ());
1864 use_min_of_max_sew (prev, next);
1865 }
1866 inline void use_next_sew_lmul (vsetvl_info &prev, const vsetvl_info &next)
1867 {
1868 use_next_sew (prev, next);
1869 prev.set_vlmul (next.get_vlmul ());
1870 prev.set_ratio (next.get_ratio ());
1871 }
1872 inline void use_next_sew_with_prev_ratio (vsetvl_info &prev,
1873 const vsetvl_info &next)
1874 {
1875 use_next_sew (prev, next);
1876 prev.set_vlmul (calculate_vlmul (next.get_sew (), prev.get_ratio ()));
1877 }
1878 inline void modify_lmul_with_next_ratio (vsetvl_info &prev,
1879 const vsetvl_info &next)
1880 {
1881 prev.set_vlmul (calculate_vlmul (prev.get_sew (), next.get_ratio ()));
1882 prev.set_ratio (next.get_ratio ());
1883 }
1884
1885 inline void use_max_sew_and_lmul_with_next_ratio (vsetvl_info &prev,
1886 const vsetvl_info &next)
1887 {
1888 int max_sew = MAX (prev.get_sew (), next.get_sew ());
1889 prev.set_vlmul (calculate_vlmul (max_sew, next.get_ratio ()));
1890 use_max_sew (prev, next);
1891 prev.set_ratio (next.get_ratio ());
1892 }
1893
1894 inline void use_max_sew_and_lmul_with_prev_ratio (vsetvl_info &prev,
1895 const vsetvl_info &next)
1896 {
1897 int max_sew = MAX (prev.get_sew (), next.get_sew ());
1898 prev.set_vlmul (calculate_vlmul (max_sew, prev.get_ratio ()));
1899 prev.set_sew (max_sew);
1900 }
1901
1902 /* modifiers for tail and mask policy */
1903
1904 inline void use_tail_policy (vsetvl_info &prev, const vsetvl_info &next)
1905 {
1906 if (!next.get_ta ())
1907 prev.set_ta (next.get_ta ());
1908 }
1909 inline void use_mask_policy (vsetvl_info &prev, const vsetvl_info &next)
1910 {
1911 if (!next.get_ma ())
1912 prev.set_ma (next.get_ma ());
1913 }
1914 inline void use_tail_mask_policy (vsetvl_info &prev, const vsetvl_info &next)
1915 {
1916 use_tail_policy (prev, next);
1917 use_mask_policy (prev, next);
1918 }
1919
1920 /* modifiers for avl */
1921
1922 inline void use_next_avl (vsetvl_info &prev, const vsetvl_info &next)
1923 {
1924 gcc_assert (can_use_next_avl_p (prev, next));
1925 prev.update_avl (next);
1926 }
1927
1928 inline void use_next_avl_when_not_equal (vsetvl_info &prev,
1929 const vsetvl_info &next)
1930 {
1931 if (avl_equal_p (prev, next))
1932 return;
1933 gcc_assert (next.has_non_zero_avl ());
1934 use_next_avl (prev, next);
1935 }
1936
1937 public:
1938 /* Can we move vsetvl info between prev_insn and next_insn safe? */
1939 bool avl_vl_unmodified_between_p (insn_info *prev_insn, insn_info *next_insn,
1940 const vsetvl_info &info,
1941 bool ignore_vl = false)
1942 {
1943 gcc_assert ((ignore_vl && info.has_nonvlmax_reg_avl ())
1944 || (info.has_nonvlmax_reg_avl () || info.has_vl ()));
1945
1946 gcc_assert (!prev_insn->is_debug_insn () && !next_insn->is_debug_insn ());
1947 if (prev_insn->bb () == next_insn->bb ()
1948 && prev_insn->compare_with (next_insn) < 0)
1949 {
1950 for (insn_info *i = next_insn->prev_nondebug_insn (); i != prev_insn;
1951 i = i->prev_nondebug_insn ())
1952 {
1953 // no def and use of vl
1954 if (!ignore_vl && modify_or_use_vl_p (i, info))
1955 return false;
1956
1957 // no def of avl
1958 if (modify_avl_p (i, info))
1959 return false;
1960 }
1961 return true;
1962 }
1963 else
1964 {
1965 basic_block prev_cfg_bb = prev_insn->bb ()->cfg_bb ();
1966 if (!ignore_vl && info.has_vl ())
1967 {
1968 bitmap live_out = df_get_live_out (prev_cfg_bb);
1969 if (bitmap_bit_p (live_out, REGNO (info.get_vl ())))
1970 return false;
1971 }
1972
1973 /* Find set_info at location of PREV_INSN and NEXT_INSN, Return
1974 false if those 2 set_info are different.
1975
1976 PREV_INSN --- multiple nested blocks --- NEXT_INSN.
1977
1978 Return false if there is any modifications of AVL inside those
1979 multiple nested blocks. */
1980 if (info.has_nonvlmax_reg_avl ())
1981 {
1982 resource_info resource = full_register (REGNO (info.get_avl ()));
1983 def_lookup dl1 = crtl->ssa->find_def (resource, prev_insn);
1984 def_lookup dl2 = crtl->ssa->find_def (resource, next_insn);
1985 if (dl2.matching_set ())
1986 return false;
1987
1988 auto is_phi_or_real
1989 = [&] (insn_info *h) { return h->is_real () || h->is_phi (); };
1990
1991 def_info *def1 = dl1.matching_set_or_last_def_of_prev_group ();
1992 def_info *def2 = dl2.prev_def (next_insn);
1993 set_info *set1 = safe_dyn_cast<set_info *> (def1);
1994 set_info *set2 = safe_dyn_cast<set_info *> (def2);
1995 if (!set1 || !set2)
1996 return false;
1997
1998 auto is_same_ultimate_def = [&] (set_info *s1, set_info *s2) {
1999 return s1->insn ()->is_phi () && s2->insn ()->is_phi ()
2000 && look_through_degenerate_phi (s1)
2001 == look_through_degenerate_phi (s2);
2002 };
2003
2004 if (set1 != set2 && !is_same_ultimate_def (set1, set2))
2005 {
2006 if (!is_phi_or_real (set1->insn ())
2007 || !is_phi_or_real (set2->insn ()))
2008 return false;
2009
2010 if (set1->insn ()->is_real () && set2->insn ()->is_phi ())
2011 {
2012 hash_set<set_info *> sets
2013 = get_all_sets (set2, true, false, true);
2014 if (!sets.contains (set1))
2015 return false;
2016 }
2017 else
2018 {
2019 insn_info *def_insn1 = extract_single_source (set1);
2020 insn_info *def_insn2 = extract_single_source (set2);
2021 if (!def_insn1 || !def_insn2 || def_insn1 != def_insn2)
2022 return false;
2023 }
2024 }
2025 }
2026
2027 for (insn_info *i = next_insn; i != next_insn->bb ()->head_insn ();
2028 i = i->prev_nondebug_insn ())
2029 {
2030 // no def and use of vl
2031 if (!ignore_vl && modify_or_use_vl_p (i, info))
2032 return false;
2033
2034 // no def of avl
2035 if (modify_avl_p (i, info))
2036 return false;
2037 }
2038
2039 for (insn_info *i = prev_insn->bb ()->end_insn (); i != prev_insn;
2040 i = i->prev_nondebug_insn ())
2041 {
2042 // no def mad use of vl
2043 if (!ignore_vl && modify_or_use_vl_p (i, info))
2044 return false;
2045
2046 // no def of avl
2047 if (modify_avl_p (i, info))
2048 return false;
2049 }
2050 }
2051 return true;
2052 }
2053
2054 bool sew_lmul_compatible_p (const vsetvl_info &prev, const vsetvl_info &next)
2055 {
2056 gcc_assert (prev.valid_p () && next.valid_p ());
2057 sew_lmul_demand_type prev_flags = prev.get_sew_lmul_demand ();
2058 sew_lmul_demand_type next_flags = next.get_sew_lmul_demand ();
2059 #define DEF_SEW_LMUL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2060 AVAILABLE_P, FUSE) \
2061 if (prev_flags == sew_lmul_demand_type::PREV_FLAGS \
2062 && next_flags == sew_lmul_demand_type::NEXT_FLAGS) \
2063 return COMPATIBLE_P (prev, next);
2064
2065 #include "riscv-vsetvl.def"
2066
2067 gcc_unreachable ();
2068 }
2069
2070 bool sew_lmul_available_p (const vsetvl_info &prev, const vsetvl_info &next)
2071 {
2072 gcc_assert (prev.valid_p () && next.valid_p ());
2073 sew_lmul_demand_type prev_flags = prev.get_sew_lmul_demand ();
2074 sew_lmul_demand_type next_flags = next.get_sew_lmul_demand ();
2075 #define DEF_SEW_LMUL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2076 AVAILABLE_P, FUSE) \
2077 if (prev_flags == sew_lmul_demand_type::PREV_FLAGS \
2078 && next_flags == sew_lmul_demand_type::NEXT_FLAGS) \
2079 return AVAILABLE_P (prev, next);
2080
2081 #include "riscv-vsetvl.def"
2082
2083 gcc_unreachable ();
2084 }
2085
2086 void merge_sew_lmul (vsetvl_info &prev, const vsetvl_info &next)
2087 {
2088 gcc_assert (prev.valid_p () && next.valid_p ());
2089 sew_lmul_demand_type prev_flags = prev.get_sew_lmul_demand ();
2090 sew_lmul_demand_type next_flags = next.get_sew_lmul_demand ();
2091 #define DEF_SEW_LMUL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2092 AVAILABLE_P, FUSE) \
2093 if (prev_flags == sew_lmul_demand_type::PREV_FLAGS \
2094 && next_flags == sew_lmul_demand_type::NEXT_FLAGS) \
2095 { \
2096 gcc_assert (COMPATIBLE_P (prev, next)); \
2097 FUSE (prev, next); \
2098 prev.set_sew_lmul_demand (sew_lmul_demand_type::NEW_FLAGS); \
2099 return; \
2100 }
2101
2102 #include "riscv-vsetvl.def"
2103
2104 gcc_unreachable ();
2105 }
2106
2107 bool policy_compatible_p (const vsetvl_info &prev, const vsetvl_info &next)
2108 {
2109 gcc_assert (prev.valid_p () && next.valid_p ());
2110 policy_demand_type prev_flags = prev.get_policy_demand ();
2111 policy_demand_type next_flags = next.get_policy_demand ();
2112 #define DEF_POLICY_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2113 AVAILABLE_P, FUSE) \
2114 if (prev_flags == policy_demand_type::PREV_FLAGS \
2115 && next_flags == policy_demand_type::NEXT_FLAGS) \
2116 return COMPATIBLE_P (prev, next);
2117
2118 #include "riscv-vsetvl.def"
2119
2120 gcc_unreachable ();
2121 }
2122
2123 bool policy_available_p (const vsetvl_info &prev, const vsetvl_info &next)
2124 {
2125 gcc_assert (prev.valid_p () && next.valid_p ());
2126 policy_demand_type prev_flags = prev.get_policy_demand ();
2127 policy_demand_type next_flags = next.get_policy_demand ();
2128 #define DEF_POLICY_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2129 AVAILABLE_P, FUSE) \
2130 if (prev_flags == policy_demand_type::PREV_FLAGS \
2131 && next_flags == policy_demand_type::NEXT_FLAGS) \
2132 return AVAILABLE_P (prev, next);
2133
2134 #include "riscv-vsetvl.def"
2135
2136 gcc_unreachable ();
2137 }
2138
2139 void merge_policy (vsetvl_info &prev, const vsetvl_info &next)
2140 {
2141 gcc_assert (prev.valid_p () && next.valid_p ());
2142 policy_demand_type prev_flags = prev.get_policy_demand ();
2143 policy_demand_type next_flags = next.get_policy_demand ();
2144 #define DEF_POLICY_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2145 AVAILABLE_P, FUSE) \
2146 if (prev_flags == policy_demand_type::PREV_FLAGS \
2147 && next_flags == policy_demand_type::NEXT_FLAGS) \
2148 { \
2149 gcc_assert (COMPATIBLE_P (prev, next)); \
2150 FUSE (prev, next); \
2151 prev.set_policy_demand (policy_demand_type::NEW_FLAGS); \
2152 return; \
2153 }
2154
2155 #include "riscv-vsetvl.def"
2156
2157 gcc_unreachable ();
2158 }
2159
2160 bool vl_not_in_conflict_p (const vsetvl_info &prev, const vsetvl_info &next)
2161 {
2162 /* We don't fuse this following case:
2163
2164 li a5, -1
2165 vmv.s.x v0, a5 -- PREV
2166 vsetvli a5, ... -- NEXT
2167
2168 Don't fuse NEXT into PREV.
2169 */
2170 return !prev.vl_modify_non_avl_op_p (next)
2171 && !next.vl_modify_non_avl_op_p (prev);
2172 }
2173
2174 bool avl_compatible_p (const vsetvl_info &prev, const vsetvl_info &next)
2175 {
2176 gcc_assert (prev.valid_p () && next.valid_p ());
2177 avl_demand_type prev_flags = prev.get_avl_demand ();
2178 avl_demand_type next_flags = next.get_avl_demand ();
2179 #define DEF_AVL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2180 AVAILABLE_P, FUSE) \
2181 if (prev_flags == avl_demand_type::PREV_FLAGS \
2182 && next_flags == avl_demand_type::NEXT_FLAGS) \
2183 return COMPATIBLE_P (prev, next);
2184
2185 #include "riscv-vsetvl.def"
2186
2187 gcc_unreachable ();
2188 }
2189
2190 bool avl_available_p (const vsetvl_info &prev, const vsetvl_info &next)
2191 {
2192 gcc_assert (prev.valid_p () && next.valid_p ());
2193 avl_demand_type prev_flags = prev.get_avl_demand ();
2194 avl_demand_type next_flags = next.get_avl_demand ();
2195 #define DEF_AVL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2196 AVAILABLE_P, FUSE) \
2197 if (prev_flags == avl_demand_type::PREV_FLAGS \
2198 && next_flags == avl_demand_type::NEXT_FLAGS) \
2199 return AVAILABLE_P (prev, next);
2200
2201 #include "riscv-vsetvl.def"
2202
2203 gcc_unreachable ();
2204 }
2205
2206 void merge_avl (vsetvl_info &prev, const vsetvl_info &next)
2207 {
2208 gcc_assert (prev.valid_p () && next.valid_p ());
2209 avl_demand_type prev_flags = prev.get_avl_demand ();
2210 avl_demand_type next_flags = next.get_avl_demand ();
2211 #define DEF_AVL_RULE(PREV_FLAGS, NEXT_FLAGS, NEW_FLAGS, COMPATIBLE_P, \
2212 AVAILABLE_P, FUSE) \
2213 if (prev_flags == avl_demand_type::PREV_FLAGS \
2214 && next_flags == avl_demand_type::NEXT_FLAGS) \
2215 { \
2216 gcc_assert (COMPATIBLE_P (prev, next)); \
2217 FUSE (prev, next); \
2218 prev.set_avl_demand (avl_demand_type::NEW_FLAGS); \
2219 return; \
2220 }
2221
2222 #include "riscv-vsetvl.def"
2223
2224 gcc_unreachable ();
2225 }
2226
2227 bool compatible_p (const vsetvl_info &prev, const vsetvl_info &next)
2228 {
2229 bool compatible_p = sew_lmul_compatible_p (prev, next)
2230 && policy_compatible_p (prev, next)
2231 && avl_compatible_p (prev, next)
2232 && vl_not_in_conflict_p (prev, next);
2233 return compatible_p;
2234 }
2235
2236 bool available_p (const vsetvl_info &prev, const vsetvl_info &next)
2237 {
2238 bool available_p = sew_lmul_available_p (prev, next)
2239 && policy_available_p (prev, next)
2240 && avl_available_p (prev, next)
2241 && vl_not_in_conflict_p (prev, next);
2242 gcc_assert (!available_p || compatible_p (prev, next));
2243 return available_p;
2244 }
2245
2246 void merge (vsetvl_info &prev, const vsetvl_info &next)
2247 {
2248 gcc_assert (compatible_p (prev, next));
2249 merge_sew_lmul (prev, next);
2250 merge_policy (prev, next);
2251 merge_avl (prev, next);
2252 gcc_assert (available_p (prev, next));
2253 }
2254 };
2255
2256
2257 class pre_vsetvl
2258 {
2259 private:
2260 demand_system m_dem;
2261 auto_vec<vsetvl_block_info> m_vector_block_infos;
2262
2263 /* data for avl reaching definition. */
2264 sbitmap *m_reg_def_loc;
2265
2266 /* Holds register uses per basic block. Restricted to those registers that
2267 are used as vsetvl destinations. */
2268 sbitmap *m_reg_use_loc;
2269
2270 /* data for vsetvl info reaching definition. */
2271 vsetvl_info m_unknown_info;
2272 auto_vec<vsetvl_info *> m_vsetvl_def_exprs;
2273 sbitmap *m_vsetvl_def_in;
2274 sbitmap *m_vsetvl_def_out;
2275
2276 /* Reaching data for vsetvl AVL operands. */
2277 sbitmap *m_vsetvl_avl_reach_in;
2278 sbitmap *m_vsetvl_avl_reach_out;
2279
2280 /* data for lcm */
2281 auto_vec<vsetvl_info *> m_exprs;
2282 sbitmap *m_avloc;
2283 sbitmap *m_avin;
2284 sbitmap *m_avout;
2285 sbitmap *m_kill;
2286 sbitmap *m_antloc;
2287 sbitmap *m_transp;
2288 sbitmap *m_insert;
2289 sbitmap *m_del;
2290 struct edge_list *m_edges;
2291
2292 auto_vec<vsetvl_info> m_delete_list;
2293
2294 vsetvl_block_info &get_block_info (const bb_info *bb)
2295 {
2296 return m_vector_block_infos[bb->index ()];
2297 }
2298 const vsetvl_block_info &get_block_info (const basic_block bb) const
2299 {
2300 return m_vector_block_infos[bb->index];
2301 }
2302
2303 vsetvl_block_info &get_block_info (const basic_block bb)
2304 {
2305 return m_vector_block_infos[bb->index];
2306 }
2307
2308 void add_expr (auto_vec<vsetvl_info *> &m_exprs, vsetvl_info &info)
2309 {
2310 for (vsetvl_info *item : m_exprs)
2311 {
2312 if (*item == info)
2313 return;
2314 }
2315 m_exprs.safe_push (&info);
2316 }
2317
2318 unsigned get_expr_index (auto_vec<vsetvl_info *> &m_exprs,
2319 const vsetvl_info &info)
2320 {
2321 for (size_t i = 0; i < m_exprs.length (); i += 1)
2322 {
2323 if (*m_exprs[i] == info)
2324 return i;
2325 }
2326 gcc_unreachable ();
2327 }
2328
2329 bool anticipated_exp_p (const vsetvl_info &header_info)
2330 {
2331 if (!header_info.has_nonvlmax_reg_avl () && !header_info.has_vl ())
2332 return true;
2333
2334 bb_info *bb = header_info.get_bb ();
2335 insn_info *prev_insn = bb->head_insn ();
2336 insn_info *next_insn = header_info.insn_inside_bb_p ()
2337 ? header_info.get_insn ()
2338 : header_info.get_bb ()->end_insn ();
2339
2340 return m_dem.avl_vl_unmodified_between_p (prev_insn, next_insn,
2341 header_info);
2342 }
2343
2344 bool available_exp_p (const vsetvl_info &prev_info,
2345 const vsetvl_info &next_info)
2346 {
2347 return m_dem.available_p (prev_info, next_info);
2348 }
2349
2350 void compute_probabilities ()
2351 {
2352 edge e;
2353 edge_iterator ei;
2354
2355 for (const bb_info *bb : crtl->ssa->bbs ())
2356 {
2357 basic_block cfg_bb = bb->cfg_bb ();
2358 auto &curr_prob = get_block_info (cfg_bb).probability;
2359
2360 /* GCC assume entry block (bb 0) are always so
2361 executed so set its probability as "always". */
2362 if (ENTRY_BLOCK_PTR_FOR_FN (cfun) == cfg_bb)
2363 curr_prob = profile_probability::always ();
2364 /* Exit block (bb 1) is the block we don't need to process. */
2365 if (EXIT_BLOCK_PTR_FOR_FN (cfun) == cfg_bb)
2366 continue;
2367
2368 gcc_assert (curr_prob.initialized_p ());
2369 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
2370 {
2371 auto &new_prob = get_block_info (e->dest).probability;
2372 /* Normally, the edge probability should be initialized.
2373 However, some special testing code which is written in
2374 GIMPLE IR style force the edge probability uninitialized,
2375 we conservatively set it as never so that it will not
2376 affect PRE (Phase 3 && Phase 4). */
2377 if (!e->probability.initialized_p ())
2378 new_prob = profile_probability::never ();
2379 else if (!new_prob.initialized_p ())
2380 new_prob = curr_prob * e->probability;
2381 else if (new_prob == profile_probability::always ())
2382 continue;
2383 else
2384 new_prob += curr_prob * e->probability;
2385 }
2386 }
2387 }
2388
2389 void insert_vsetvl_insn (enum emit_type emit_type, const vsetvl_info &info)
2390 {
2391 rtx pat = info.get_vsetvl_pat ();
2392 rtx_insn *rinsn = info.get_insn ()->rtl ();
2393
2394 if (emit_type == EMIT_DIRECT)
2395 {
2396 emit_insn (pat);
2397 if (dump_file)
2398 {
2399 fprintf (dump_file, " Insert vsetvl insn %d:\n",
2400 INSN_UID (get_last_insn ()));
2401 print_rtl_single (dump_file, get_last_insn ());
2402 }
2403 }
2404 else if (emit_type == EMIT_BEFORE)
2405 {
2406 emit_insn_before (pat, rinsn);
2407 if (dump_file)
2408 {
2409 fprintf (dump_file, " Insert vsetvl insn before insn %d:\n",
2410 INSN_UID (rinsn));
2411 print_rtl_single (dump_file, PREV_INSN (rinsn));
2412 }
2413 }
2414 else
2415 {
2416 emit_insn_after (pat, rinsn);
2417 if (dump_file)
2418 {
2419 fprintf (dump_file, " Insert vsetvl insn after insn %d:\n",
2420 INSN_UID (rinsn));
2421 print_rtl_single (dump_file, NEXT_INSN (rinsn));
2422 }
2423 }
2424 }
2425
2426 void change_vsetvl_insn (const vsetvl_info &info)
2427 {
2428 rtx_insn *rinsn = info.get_insn ()->rtl ();
2429 rtx new_pat = info.get_vsetvl_pat ();
2430
2431 if (dump_file)
2432 {
2433 fprintf (dump_file, " Change insn %d from:\n", INSN_UID (rinsn));
2434 print_rtl_single (dump_file, rinsn);
2435 }
2436
2437 validate_change_or_fail (rinsn, &PATTERN (rinsn), new_pat, false);
2438
2439 if (dump_file)
2440 {
2441 fprintf (dump_file, "\n to:\n");
2442 print_rtl_single (dump_file, rinsn);
2443 }
2444 }
2445
2446 void remove_vsetvl_insn (rtx_insn *rinsn)
2447 {
2448 if (dump_file)
2449 {
2450 fprintf (dump_file, " Eliminate insn %d:\n", INSN_UID (rinsn));
2451 print_rtl_single (dump_file, rinsn);
2452 }
2453 if (in_sequence_p ())
2454 remove_insn (rinsn);
2455 else
2456 delete_insn (rinsn);
2457 }
2458
2459 bool successors_probability_equal_p (const basic_block cfg_bb) const
2460 {
2461 edge e;
2462 edge_iterator ei;
2463 profile_probability prob = profile_probability::uninitialized ();
2464 FOR_EACH_EDGE (e, ei, cfg_bb->succs)
2465 {
2466 if (prob == profile_probability::uninitialized ())
2467 prob = m_vector_block_infos[e->dest->index].probability;
2468 else if (prob == m_vector_block_infos[e->dest->index].probability)
2469 continue;
2470 else
2471 /* We pick the highest probability among those incompatible VSETVL
2472 infos. When all incompatible VSETVL infos have same probability, we
2473 don't pick any of them. */
2474 return false;
2475 }
2476 return true;
2477 }
2478
2479 bool has_compatible_reaching_vsetvl_p (vsetvl_info info)
2480 {
2481 unsigned int index;
2482 sbitmap_iterator sbi;
2483 EXECUTE_IF_SET_IN_BITMAP (m_vsetvl_def_in[info.get_bb ()->index ()], 0,
2484 index, sbi)
2485 {
2486 const auto prev_info = *m_vsetvl_def_exprs[index];
2487 if (!prev_info.valid_p ())
2488 continue;
2489 if (m_dem.compatible_p (prev_info, info))
2490 return true;
2491 }
2492 return false;
2493 }
2494
2495 bool preds_all_same_avl_and_ratio_p (const vsetvl_info &curr_info)
2496 {
2497 gcc_assert (
2498 !bitmap_empty_p (m_vsetvl_def_in[curr_info.get_bb ()->index ()]));
2499
2500 unsigned expr_index;
2501 sbitmap_iterator sbi;
2502 EXECUTE_IF_SET_IN_BITMAP (m_vsetvl_def_in[curr_info.get_bb ()->index ()], 0,
2503 expr_index, sbi)
2504 {
2505 const vsetvl_info &prev_info = *m_vsetvl_def_exprs[expr_index];
2506 if (!prev_info.valid_p ()
2507 || !m_dem.avl_available_p (prev_info, curr_info)
2508 || prev_info.get_ratio () != curr_info.get_ratio ())
2509 return false;
2510 }
2511
2512 return true;
2513 }
2514
2515 public:
2516 pre_vsetvl ()
2517 : m_reg_def_loc (nullptr), m_reg_use_loc (nullptr),
2518 m_vsetvl_def_in (nullptr), m_vsetvl_def_out (nullptr),
2519 m_vsetvl_avl_reach_in (nullptr), m_vsetvl_avl_reach_out (nullptr),
2520 m_avloc (nullptr),
2521 m_avin (nullptr), m_avout (nullptr), m_kill (nullptr), m_antloc (nullptr),
2522 m_transp (nullptr), m_insert (nullptr), m_del (nullptr), m_edges (nullptr)
2523 {
2524 init_rtl_ssa ();
2525 m_vector_block_infos.safe_grow_cleared (last_basic_block_for_fn (cfun));
2526 compute_probabilities ();
2527 m_unknown_info.set_unknown ();
2528 }
2529
2530 void finish ()
2531 {
2532 finish_rtl_ssa ();
2533
2534 if (m_reg_def_loc)
2535 sbitmap_vector_free (m_reg_def_loc);
2536 if (m_reg_use_loc)
2537 sbitmap_vector_free (m_reg_use_loc);
2538
2539 if (m_vsetvl_def_in)
2540 sbitmap_vector_free (m_vsetvl_def_in);
2541 if (m_vsetvl_def_out)
2542 sbitmap_vector_free (m_vsetvl_def_out);
2543
2544 if (m_vsetvl_avl_reach_in)
2545 sbitmap_vector_free (m_vsetvl_avl_reach_in);
2546 if (m_vsetvl_avl_reach_out)
2547 sbitmap_vector_free (m_vsetvl_avl_reach_out);
2548
2549 if (m_avloc)
2550 sbitmap_vector_free (m_avloc);
2551 if (m_kill)
2552 sbitmap_vector_free (m_kill);
2553 if (m_antloc)
2554 sbitmap_vector_free (m_antloc);
2555 if (m_transp)
2556 sbitmap_vector_free (m_transp);
2557 if (m_insert)
2558 sbitmap_vector_free (m_insert);
2559 if (m_del)
2560 sbitmap_vector_free (m_del);
2561 if (m_avin)
2562 sbitmap_vector_free (m_avin);
2563 if (m_avout)
2564 sbitmap_vector_free (m_avout);
2565
2566 if (m_edges)
2567 free_edge_list (m_edges);
2568 }
2569
2570 void compute_vsetvl_def_data ();
2571 void compute_transparent (const bb_info *);
2572 void compute_lcm_local_properties ();
2573
2574 void fuse_local_vsetvl_info ();
2575 bool earliest_fuse_vsetvl_info (int iter);
2576 void pre_global_vsetvl_info ();
2577 void emit_vsetvl ();
2578 void cleanup ();
2579 void remove_avl_operand ();
2580 void remove_unused_dest_operand ();
2581 void remove_vsetvl_pre_insns ();
2582
2583 void dump (FILE *file, const char *title) const
2584 {
2585 fprintf (file, "\nVSETVL infos after %s\n\n", title);
2586 for (const bb_info *bb : crtl->ssa->bbs ())
2587 {
2588 const auto &block_info = m_vector_block_infos[bb->index ()];
2589 fprintf (file, " bb %d:\n", bb->index ());
2590 fprintf (file, " probability: ");
2591 block_info.probability.dump (file);
2592 fprintf (file, "\n");
2593 if (!block_info.empty_p ())
2594 {
2595 fprintf (file, " Header vsetvl info:");
2596 block_info.get_entry_info ().dump (file, " ");
2597 fprintf (file, " Footer vsetvl info:");
2598 block_info.get_exit_info ().dump (file, " ");
2599 for (const auto &info : block_info.local_infos)
2600 {
2601 fprintf (file,
2602 " insn %d vsetvl info:", info.get_insn ()->uid ());
2603 info.dump (file, " ");
2604 }
2605 }
2606 }
2607 }
2608 };
2609
2610 void
2611 pre_vsetvl::compute_vsetvl_def_data ()
2612 {
2613 m_vsetvl_def_exprs.truncate (0);
2614 add_expr (m_vsetvl_def_exprs, m_unknown_info);
2615 for (const bb_info *bb : crtl->ssa->bbs ())
2616 {
2617 vsetvl_block_info &block_info = get_block_info (bb);
2618 if (block_info.empty_p ())
2619 continue;
2620 vsetvl_info &footer_info = block_info.get_exit_info ();
2621 gcc_assert (footer_info.valid_p () || footer_info.unknown_p ());
2622 add_expr (m_vsetvl_def_exprs, footer_info);
2623 }
2624
2625 if (m_vsetvl_def_in)
2626 sbitmap_vector_free (m_vsetvl_def_in);
2627 if (m_vsetvl_def_out)
2628 sbitmap_vector_free (m_vsetvl_def_out);
2629 if (m_vsetvl_avl_reach_in)
2630 sbitmap_vector_free (m_vsetvl_avl_reach_in);
2631 if (m_vsetvl_avl_reach_out)
2632 sbitmap_vector_free (m_vsetvl_avl_reach_out);
2633
2634 sbitmap *def_loc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
2635 m_vsetvl_def_exprs.length ());
2636 sbitmap *m_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
2637 m_vsetvl_def_exprs.length ());
2638
2639 m_vsetvl_def_in = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
2640 m_vsetvl_def_exprs.length ());
2641 m_vsetvl_def_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
2642 m_vsetvl_def_exprs.length ());
2643
2644 m_vsetvl_avl_reach_in
2645 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), GP_REG_LAST + 1);
2646 m_vsetvl_avl_reach_out
2647 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), GP_REG_LAST + 1);
2648
2649 bitmap_vector_clear (def_loc, last_basic_block_for_fn (cfun));
2650 bitmap_vector_clear (m_kill, last_basic_block_for_fn (cfun));
2651 bitmap_vector_clear (m_vsetvl_def_out, last_basic_block_for_fn (cfun));
2652
2653 for (const bb_info *bb : crtl->ssa->bbs ())
2654 {
2655 vsetvl_block_info &block_info = get_block_info (bb);
2656 if (block_info.empty_p ())
2657 {
2658 for (unsigned i = 0; i < m_vsetvl_def_exprs.length (); i += 1)
2659 {
2660 auto *info = m_vsetvl_def_exprs[i];
2661 if (info->has_nonvlmax_reg_avl ()
2662 && bitmap_bit_p (m_reg_def_loc[bb->index ()],
2663 REGNO (info->get_avl ())))
2664 {
2665 bitmap_set_bit (m_kill[bb->index ()], i);
2666 bitmap_set_bit (def_loc[bb->index ()],
2667 get_expr_index (m_vsetvl_def_exprs,
2668 m_unknown_info));
2669 }
2670 }
2671 continue;
2672 }
2673
2674 vsetvl_info &footer_info = block_info.get_exit_info ();
2675 bitmap_ones (m_kill[bb->index ()]);
2676 bitmap_set_bit (def_loc[bb->index ()],
2677 get_expr_index (m_vsetvl_def_exprs, footer_info));
2678 }
2679
2680 /* Set the def_out of the ENTRY basic block to m_unknown_info expr. */
2681 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
2682 bitmap_set_bit (m_vsetvl_def_out[entry->index],
2683 get_expr_index (m_vsetvl_def_exprs, m_unknown_info));
2684
2685 compute_reaching_definition (def_loc, m_kill, m_vsetvl_def_in,
2686 m_vsetvl_def_out);
2687
2688 if (dump_file && (dump_flags & TDF_DETAILS))
2689 {
2690 fprintf (dump_file,
2691 "\n Compute vsetvl info reaching definition data:\n\n");
2692 fprintf (dump_file, " Expression List (%d):\n",
2693 m_vsetvl_def_exprs.length ());
2694 for (unsigned i = 0; i < m_vsetvl_def_exprs.length (); i++)
2695 {
2696 const auto &info = *m_vsetvl_def_exprs[i];
2697 fprintf (dump_file, " Expr[%u]: ", i);
2698 info.dump (dump_file, " ");
2699 }
2700 fprintf (dump_file, "\n bitmap data:\n");
2701 for (const bb_info *bb : crtl->ssa->bbs ())
2702 {
2703 unsigned int i = bb->index ();
2704 fprintf (dump_file, " BB %u:\n", i);
2705 fprintf (dump_file, " def_loc: ");
2706 dump_bitmap_file (dump_file, def_loc[i]);
2707 fprintf (dump_file, " kill: ");
2708 dump_bitmap_file (dump_file, m_kill[i]);
2709 fprintf (dump_file, " vsetvl_def_in: ");
2710 dump_bitmap_file (dump_file, m_vsetvl_def_in[i]);
2711 fprintf (dump_file, " vsetvl_def_out: ");
2712 dump_bitmap_file (dump_file, m_vsetvl_def_out[i]);
2713 }
2714 }
2715
2716 sbitmap_vector_free (def_loc);
2717 sbitmap_vector_free (m_kill);
2718
2719 /* Now compute the reaching definitions for AVL operands.
2720 We can reuse def_loc but index it by regnos now. */
2721 def_loc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
2722 GP_REG_LAST + 1);
2723
2724 bitmap_vector_clear (def_loc, last_basic_block_for_fn (cfun));
2725 bitmap_vector_clear (m_vsetvl_avl_reach_out, last_basic_block_for_fn (cfun));
2726
2727 for (const bb_info *bb : crtl->ssa->bbs ())
2728 {
2729 vsetvl_block_info &block_info = get_block_info (bb);
2730 if (block_info.empty_p ())
2731 continue;
2732 vsetvl_info &info = block_info.get_exit_info ();
2733 if (info.has_vl ())
2734 bitmap_set_bit (def_loc[bb->index ()], REGNO (info.get_vl ()));
2735 }
2736
2737 compute_reaching_definition (def_loc, m_reg_def_loc, m_vsetvl_avl_reach_in,
2738 m_vsetvl_avl_reach_out);
2739 }
2740
2741 /* Subroutine of compute_lcm_local_properties which Compute local transparent
2742 BB. Note that the compile time is very sensitive to compute_transparent and
2743 compute_lcm_local_properties, any change of these 2 functions should be
2744 aware of the compile time changing of the program which has a large number of
2745 blocks, e.g SPEC 2017 wrf.
2746
2747 Current compile time profile of SPEC 2017 wrf:
2748
2749 1. scheduling - 27%
2750 2. machine dep reorg (VSETVL PASS) - 18%
2751
2752 VSETVL pass should not spend more time than scheduling in compilation. */
2753 void
2754 pre_vsetvl::compute_transparent (const bb_info *bb)
2755 {
2756 int num_exprs = m_exprs.length ();
2757 unsigned bb_index = bb->index ();
2758 for (int i = 0; i < num_exprs; i++)
2759 {
2760 auto *info = m_exprs[i];
2761 if (info->has_nonvlmax_reg_avl ()
2762 && bitmap_bit_p (m_reg_def_loc[bb_index], REGNO (info->get_avl ())))
2763 bitmap_clear_bit (m_transp[bb_index], i);
2764 else if (info->has_vl ())
2765 {
2766 /* If the VL reg is redefined, we cannot move a vsetvl past it. */
2767 if (bitmap_bit_p (m_reg_def_loc[bb_index],
2768 REGNO (info->get_vl ())))
2769 bitmap_clear_bit (m_transp[bb_index], i);
2770 /* Same if there is a VL reg use that didn't come from a vsetvl. */
2771 else if (bitmap_bit_p (m_reg_use_loc[bb_index],
2772 REGNO (info->get_vl ()))
2773 && !bitmap_bit_p (m_vsetvl_avl_reach_in[bb_index],
2774 REGNO (info->get_vl ())))
2775 bitmap_clear_bit (m_transp[bb_index], i);
2776 }
2777 }
2778 }
2779
2780 /* Compute the local properties of each recorded expression.
2781
2782 Local properties are those that are defined by the block, irrespective of
2783 other blocks.
2784
2785 An expression is transparent in a block if its operands are not modified
2786 in the block.
2787
2788 An expression is computed (locally available) in a block if it is computed
2789 at least once and expression would contain the same value if the
2790 computation was moved to the end of the block.
2791
2792 An expression is locally anticipatable in a block if it is computed at
2793 least once and expression would contain the same value if the computation
2794 was moved to the beginning of the block. */
2795 void
2796 pre_vsetvl::compute_lcm_local_properties ()
2797 {
2798 m_exprs.truncate (0);
2799 for (const bb_info *bb : crtl->ssa->bbs ())
2800 {
2801 vsetvl_block_info &block_info = get_block_info (bb);
2802 if (block_info.empty_p ())
2803 continue;
2804 vsetvl_info &header_info = block_info.get_entry_info ();
2805 vsetvl_info &footer_info = block_info.get_exit_info ();
2806 gcc_assert (footer_info.valid_p () || footer_info.unknown_p ());
2807 if (header_info.valid_p ())
2808 add_expr (m_exprs, header_info);
2809 if (footer_info.valid_p ())
2810 add_expr (m_exprs, footer_info);
2811 }
2812
2813 int num_exprs = m_exprs.length ();
2814 if (m_avloc)
2815 sbitmap_vector_free (m_avloc);
2816 if (m_kill)
2817 sbitmap_vector_free (m_kill);
2818 if (m_antloc)
2819 sbitmap_vector_free (m_antloc);
2820 if (m_transp)
2821 sbitmap_vector_free (m_transp);
2822 if (m_avin)
2823 sbitmap_vector_free (m_avin);
2824 if (m_avout)
2825 sbitmap_vector_free (m_avout);
2826
2827 m_avloc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2828 m_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2829 m_antloc = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2830 m_transp = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2831 m_avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2832 m_avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
2833
2834 bitmap_vector_clear (m_avloc, last_basic_block_for_fn (cfun));
2835 bitmap_vector_clear (m_kill, last_basic_block_for_fn (cfun));
2836 bitmap_vector_clear (m_antloc, last_basic_block_for_fn (cfun));
2837 bitmap_vector_ones (m_transp, last_basic_block_for_fn (cfun));
2838
2839 /* - If T is locally available at the end of a block, then T' must be
2840 available at the end of the same block. Since some optimization has
2841 occurred earlier, T' might not be locally available, however, it must
2842 have been previously computed on all paths. As a formula, T at AVLOC(B)
2843 implies that T' at AVOUT(B).
2844 An "available occurrence" is one that is the last occurrence in the
2845 basic block and the operands are not modified by following statements in
2846 the basic block [including this insn].
2847
2848 - If T is locally anticipated at the beginning of a block, then either
2849 T', is locally anticipated or it is already available from previous
2850 blocks. As a formula, this means that T at ANTLOC(B) implies that T' at
2851 ANTLOC(B) at AVIN(B).
2852 An "anticipatable occurrence" is one that is the first occurrence in the
2853 basic block, the operands are not modified in the basic block prior
2854 to the occurrence and the output is not used between the start of
2855 the block and the occurrence. */
2856 for (const bb_info *bb : crtl->ssa->bbs ())
2857 {
2858 unsigned bb_index = bb->index ();
2859 vsetvl_block_info &block_info = get_block_info (bb);
2860
2861 /* Compute m_transp */
2862 if (block_info.empty_p ())
2863 compute_transparent (bb);
2864 else
2865 {
2866 bitmap_clear (m_transp[bb_index]);
2867 vsetvl_info &header_info = block_info.get_entry_info ();
2868 vsetvl_info &footer_info = block_info.get_exit_info ();
2869
2870 if (header_info.valid_p () && anticipated_exp_p (header_info))
2871 bitmap_set_bit (m_antloc[bb_index],
2872 get_expr_index (m_exprs, header_info));
2873
2874 if (footer_info.valid_p ())
2875 for (int i = 0; i < num_exprs; i += 1)
2876 {
2877 const vsetvl_info &info = *m_exprs[i];
2878 if (!info.valid_p ())
2879 continue;
2880 if (available_exp_p (footer_info, info))
2881 bitmap_set_bit (m_avloc[bb_index], i);
2882 }
2883 }
2884
2885 if (invalid_opt_bb_p (bb->cfg_bb ()))
2886 {
2887 if (dump_file && (dump_flags & TDF_DETAILS))
2888 fprintf (dump_file, "\n --- skipping bb %u due to weird edge",
2889 bb->index ());
2890
2891 bitmap_clear (m_antloc[bb_index]);
2892 bitmap_clear (m_transp[bb_index]);
2893 }
2894
2895 /* Compute ae_kill for each basic block using:
2896
2897 ~(TRANSP | COMP)
2898 */
2899 bitmap_ior (m_kill[bb_index], m_transp[bb_index], m_avloc[bb_index]);
2900 bitmap_not (m_kill[bb_index], m_kill[bb_index]);
2901 }
2902 }
2903
2904 void
2905 pre_vsetvl::fuse_local_vsetvl_info ()
2906 {
2907 m_reg_def_loc
2908 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), GP_REG_LAST + 1);
2909 bitmap_vector_clear (m_reg_def_loc, last_basic_block_for_fn (cfun));
2910 bitmap_ones (m_reg_def_loc[ENTRY_BLOCK_PTR_FOR_FN (cfun)->index]);
2911
2912 m_reg_use_loc
2913 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), GP_REG_LAST + 1);
2914 bitmap_vector_clear (m_reg_use_loc, last_basic_block_for_fn (cfun));
2915
2916 /* No need to track all GPRs, just use those that are VL destinations.
2917 Store them in a bitmap for filtering the uses later on. */
2918 auto_bitmap vsetvl_dest_regs;
2919 for (bb_info *bb : crtl->ssa->bbs ())
2920 for (insn_info *insn : bb->real_nondebug_insns ())
2921 {
2922 vsetvl_info info = vsetvl_info (insn);
2923 if (info.valid_p () && info.has_vl ())
2924 bitmap_set_bit (vsetvl_dest_regs, REGNO (info.get_vl ()));
2925 }
2926
2927 for (bb_info *bb : crtl->ssa->bbs ())
2928 {
2929 auto &block_info = get_block_info (bb);
2930 block_info.bb = bb;
2931 if (dump_file && (dump_flags & TDF_DETAILS))
2932 {
2933 fprintf (dump_file, " Try fuse basic block %d\n", bb->index ());
2934 }
2935 auto_vec<vsetvl_info> infos;
2936 for (insn_info *insn : bb->real_nondebug_insns ())
2937 {
2938 vsetvl_info curr_info = vsetvl_info (insn);
2939 if (curr_info.valid_p () || curr_info.unknown_p ())
2940 infos.safe_push (curr_info);
2941
2942 if (insn->is_real ())
2943 {
2944 /* Collect GPRs modified by the current bb. */
2945 for (def_info *def : insn->defs ())
2946 if (def->is_reg () && GP_REG_P (def->regno ()))
2947 bitmap_set_bit (m_reg_def_loc[bb->index ()], def->regno ());
2948 /* Collect non-vsetvl uses of GPRs. */
2949 if (!curr_info.valid_p ())
2950 {
2951 for (use_info *use : insn->uses ())
2952 if (use->is_reg () && GP_REG_P (use->regno ())
2953 && bitmap_bit_p (vsetvl_dest_regs, use->regno ()))
2954 bitmap_set_bit (m_reg_use_loc[bb->index ()],
2955 use->regno ());
2956 }
2957 }
2958 }
2959
2960 vsetvl_info prev_info = vsetvl_info ();
2961 prev_info.set_empty ();
2962 for (auto &curr_info : infos)
2963 {
2964 if (prev_info.empty_p ())
2965 prev_info = curr_info;
2966 else if ((curr_info.unknown_p () && prev_info.valid_p ())
2967 || (curr_info.valid_p () && prev_info.unknown_p ()))
2968 {
2969 block_info.local_infos.safe_push (prev_info);
2970 prev_info = curr_info;
2971 }
2972 else if (curr_info.valid_p () && prev_info.valid_p ())
2973 {
2974 if (m_dem.available_p (prev_info, curr_info))
2975 {
2976 if (dump_file && (dump_flags & TDF_DETAILS))
2977 {
2978 fprintf (dump_file,
2979 " Ignore curr info since prev info "
2980 "available with it:\n");
2981 fprintf (dump_file, " prev_info: ");
2982 prev_info.dump (dump_file, " ");
2983 fprintf (dump_file, " curr_info: ");
2984 curr_info.dump (dump_file, " ");
2985 fprintf (dump_file, "\n");
2986 }
2987 /* Even though prev_info is available with curr_info,
2988 we need to update the MAX_SEW of prev_info since
2989 we don't check MAX_SEW in available_p check.
2990
2991 prev_info:
2992 Demand fields: demand_ratio_and_ge_sew demand_avl
2993 SEW=16, VLMUL=mf4, RATIO=64, MAX_SEW=64
2994
2995 curr_info:
2996 Demand fields: demand_ge_sew demand_non_zero_avl
2997 SEW=16, VLMUL=m1, RATIO=16, MAX_SEW=32
2998
2999 In the example above, prev_info is available with
3000 curr_info, we need to update prev_info MAX_SEW from
3001 64 into 32. */
3002 prev_info.set_max_sew (
3003 MIN (prev_info.get_max_sew (), curr_info.get_max_sew ()));
3004
3005 /* If we fuse and the current, to be deleted vsetvl has uses
3006 of its VL as AVL operand in other vsetvls those will be
3007 orphaned. Therefore only delete if that's not the case.
3008 */
3009 rtx cur_dest = curr_info.has_vl ()
3010 ? curr_info.get_vl ()
3011 : NULL_RTX;
3012
3013 if (vsetvl_insn_p (curr_info.get_insn ()->rtl ())
3014 && !reg_single_use_in_avl (curr_info.get_insn (),
3015 cur_dest))
3016 m_delete_list.safe_push (curr_info);
3017
3018 if (curr_info.get_read_vl_insn ())
3019 prev_info.set_read_vl_insn (curr_info.get_read_vl_insn ());
3020 }
3021 else if (m_dem.compatible_p (prev_info, curr_info))
3022 {
3023 if (dump_file && (dump_flags & TDF_DETAILS))
3024 {
3025 fprintf (dump_file, " Fuse curr info since prev info "
3026 "compatible with it:\n");
3027 fprintf (dump_file, " prev_info: ");
3028 prev_info.dump (dump_file, " ");
3029 fprintf (dump_file, " curr_info: ");
3030 curr_info.dump (dump_file, " ");
3031 }
3032 m_dem.merge (prev_info, curr_info);
3033 if (!curr_info.vl_used_by_non_rvv_insn_p ()
3034 && vsetvl_insn_p (curr_info.get_insn ()->rtl ()))
3035 m_delete_list.safe_push (curr_info);
3036 if (curr_info.get_read_vl_insn ())
3037 prev_info.set_read_vl_insn (curr_info.get_read_vl_insn ());
3038 if (dump_file && (dump_flags & TDF_DETAILS))
3039 {
3040 fprintf (dump_file, " prev_info after fused: ");
3041 prev_info.dump (dump_file, " ");
3042 fprintf (dump_file, "\n");
3043 }
3044 }
3045 else
3046 {
3047 if (dump_file && (dump_flags & TDF_DETAILS))
3048 {
3049 fprintf (dump_file,
3050 " Cannot fuse incompatible infos:\n");
3051 fprintf (dump_file, " prev_info: ");
3052 prev_info.dump (dump_file, " ");
3053 fprintf (dump_file, " curr_info: ");
3054 curr_info.dump (dump_file, " ");
3055 }
3056 block_info.local_infos.safe_push (prev_info);
3057 prev_info = curr_info;
3058 }
3059 }
3060 }
3061
3062 if (prev_info.valid_p () || prev_info.unknown_p ())
3063 block_info.local_infos.safe_push (prev_info);
3064 }
3065 }
3066
3067
3068 bool
3069 pre_vsetvl::earliest_fuse_vsetvl_info (int iter)
3070 {
3071 compute_vsetvl_def_data ();
3072 compute_lcm_local_properties ();
3073
3074 unsigned num_exprs = m_exprs.length ();
3075 struct edge_list *m_edges = create_edge_list ();
3076 unsigned num_edges = NUM_EDGES (m_edges);
3077 sbitmap *antin
3078 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
3079 sbitmap *antout
3080 = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), num_exprs);
3081
3082 sbitmap *earliest = sbitmap_vector_alloc (num_edges, num_exprs);
3083
3084 compute_available (m_avloc, m_kill, m_avout, m_avin);
3085 compute_antinout_edge (m_antloc, m_transp, antin, antout);
3086 compute_earliest (m_edges, num_exprs, antin, antout, m_avout, m_kill,
3087 earliest);
3088
3089 if (dump_file && (dump_flags & TDF_DETAILS))
3090 {
3091 fprintf (dump_file, "\n Compute LCM earliest insert data (lift %d):\n\n",
3092 iter);
3093 fprintf (dump_file, " Expression List (%u):\n", num_exprs);
3094 for (unsigned i = 0; i < num_exprs; i++)
3095 {
3096 const auto &info = *m_exprs[i];
3097 fprintf (dump_file, " Expr[%u]: ", i);
3098 info.dump (dump_file, " ");
3099 }
3100 fprintf (dump_file, "\n bitmap data:\n");
3101 for (const bb_info *bb : crtl->ssa->bbs ())
3102 {
3103 unsigned int i = bb->index ();
3104 fprintf (dump_file, " BB %u:\n", i);
3105 fprintf (dump_file, " avloc: ");
3106 dump_bitmap_file (dump_file, m_avloc[i]);
3107 fprintf (dump_file, " kill: ");
3108 dump_bitmap_file (dump_file, m_kill[i]);
3109 fprintf (dump_file, " antloc: ");
3110 dump_bitmap_file (dump_file, m_antloc[i]);
3111 fprintf (dump_file, " transp: ");
3112 dump_bitmap_file (dump_file, m_transp[i]);
3113
3114 fprintf (dump_file, " avin: ");
3115 dump_bitmap_file (dump_file, m_avin[i]);
3116 fprintf (dump_file, " avout: ");
3117 dump_bitmap_file (dump_file, m_avout[i]);
3118 fprintf (dump_file, " antin: ");
3119 dump_bitmap_file (dump_file, antin[i]);
3120 fprintf (dump_file, " antout: ");
3121 dump_bitmap_file (dump_file, antout[i]);
3122 }
3123 fprintf (dump_file, "\n");
3124 fprintf (dump_file, " earliest:\n");
3125 for (unsigned ed = 0; ed < num_edges; ed++)
3126 {
3127 edge eg = INDEX_EDGE (m_edges, ed);
3128
3129 if (bitmap_empty_p (earliest[ed]))
3130 continue;
3131 fprintf (dump_file, " Edge(bb %u -> bb %u): ", eg->src->index,
3132 eg->dest->index);
3133 dump_bitmap_file (dump_file, earliest[ed]);
3134 }
3135 fprintf (dump_file, "\n");
3136 }
3137
3138 if (dump_file && (dump_flags & TDF_DETAILS))
3139 {
3140 fprintf (dump_file, " Fused global info result (lift %d):\n", iter);
3141 }
3142
3143 bool changed = false;
3144 for (unsigned ed = 0; ed < num_edges; ed++)
3145 {
3146 sbitmap e = earliest[ed];
3147 if (bitmap_empty_p (e))
3148 continue;
3149
3150 unsigned int expr_index;
3151 sbitmap_iterator sbi;
3152 EXECUTE_IF_SET_IN_BITMAP (e, 0, expr_index, sbi)
3153 {
3154 vsetvl_info &curr_info = *m_exprs[expr_index];
3155 edge eg = INDEX_EDGE (m_edges, ed);
3156 vsetvl_block_info &src_block_info = get_block_info (eg->src);
3157 vsetvl_block_info &dest_block_info = get_block_info (eg->dest);
3158
3159 if (!curr_info.valid_p ()
3160 || eg->probability == profile_probability::never ()
3161 || src_block_info.probability
3162 == profile_probability::uninitialized ()
3163 /* When multiple set bits in earliest edge, such edge may
3164 have infinite loop in preds or succs or multiple conflict
3165 vsetvl expression which make such edge is unrelated. We
3166 don't perform fusion for such situation. */
3167 || bitmap_count_bits (e) != 1)
3168 continue;
3169
3170 if (src_block_info.empty_p ())
3171 {
3172 vsetvl_info new_curr_info = curr_info;
3173 new_curr_info.set_bb (crtl->ssa->bb (eg->dest));
3174 bool has_compatible_p
3175 = has_compatible_reaching_vsetvl_p (new_curr_info);
3176 if (!has_compatible_p)
3177 {
3178 if (dump_file && (dump_flags & TDF_DETAILS))
3179 {
3180 fprintf (dump_file,
3181 " Forbidden lift up vsetvl info into bb %u "
3182 "since there is no vsetvl info that reaching in "
3183 "is compatible with it:",
3184 eg->src->index);
3185 curr_info.dump (dump_file, " ");
3186 }
3187 continue;
3188 }
3189
3190 /* We cannot lift a vsetvl into the source block if the block is
3191 not transparent WRT to it.
3192 This is too restrictive for blocks where a register's use only
3193 feeds into vsetvls and no regular insns. One example is the
3194 test rvv/vsetvl/avl_single-68.c which is currently XFAILed for
3195 that reason.
3196 In order to support this case we'd need to check the vsetvl's
3197 AVL operand's uses in the source block and make sure they are
3198 only used in other vsetvls. */
3199 if (!bitmap_bit_p (m_transp[eg->src->index], expr_index))
3200 continue;
3201
3202 /* Transparency tells us if we can move upwards without looking
3203 down. It is still possible to clobber non-vsetvl uses
3204 that happen to share the vsetvl destination register of the
3205 vsetvl we are about to hoist.
3206 As we have computed the vsetvl VL dest -> vsetvl AVL reach
3207 before, we can check if our VL register is live-in for each
3208 successor and not reached by a vsetvl. If so, we cannot
3209 hoist, as that would clobber the use. */
3210 if (curr_info.has_vl ())
3211 {
3212 edge succ;
3213 edge_iterator it;
3214 bool clobber = false;
3215 FOR_EACH_EDGE (succ, it, eg->src->succs)
3216 {
3217 if (succ->dest == eg->dest)
3218 continue;
3219 if (bitmap_bit_p (df_get_live_in (succ->dest),
3220 REGNO (curr_info.get_vl ()))
3221 && !bitmap_bit_p
3222 (m_vsetvl_avl_reach_in[succ->dest->index],
3223 REGNO (curr_info.get_vl ())))
3224 {
3225 clobber = true;
3226 break;
3227 }
3228 }
3229 if (clobber)
3230 continue;
3231 }
3232
3233
3234 if (dump_file && (dump_flags & TDF_DETAILS))
3235 {
3236 fprintf (dump_file,
3237 " Hoisting vsetvl info from bb %u to "
3238 "bb %u: ", eg->dest->index, eg->src->index);
3239 curr_info.dump (dump_file, " ");
3240 }
3241 src_block_info.set_info (curr_info);
3242 src_block_info.probability = dest_block_info.probability;
3243 changed = true;
3244 }
3245 else if (src_block_info.has_info ())
3246 {
3247 vsetvl_info &prev_info = src_block_info.get_exit_info ();
3248 gcc_assert (prev_info.valid_p ());
3249
3250 if (m_dem.compatible_p (prev_info, curr_info))
3251 {
3252 if (dump_file && (dump_flags & TDF_DETAILS))
3253 {
3254 fprintf (dump_file, " Fuse curr info since prev info "
3255 "compatible with it:\n");
3256 fprintf (dump_file, " prev_info: ");
3257 prev_info.dump (dump_file, " ");
3258 fprintf (dump_file, " curr_info: ");
3259 curr_info.dump (dump_file, " ");
3260 }
3261 m_dem.merge (prev_info, curr_info);
3262 if (dump_file && (dump_flags & TDF_DETAILS))
3263 {
3264 fprintf (dump_file, " prev_info after fused: ");
3265 prev_info.dump (dump_file, " ");
3266 fprintf (dump_file, "\n");
3267 }
3268 changed = true;
3269 if (src_block_info.has_info ())
3270 src_block_info.probability += dest_block_info.probability;
3271 }
3272 else
3273 {
3274 /* Cancel lift up if probabilities are equal. */
3275 if (successors_probability_equal_p (eg->src)
3276 || (dest_block_info.probability
3277 > src_block_info.probability
3278 && !has_compatible_reaching_vsetvl_p (curr_info)))
3279 {
3280 if (dump_file && (dump_flags & TDF_DETAILS))
3281 {
3282 fprintf (dump_file,
3283 " Reset bb %u:",
3284 eg->src->index);
3285 prev_info.dump (dump_file, " ");
3286 fprintf (dump_file, " due to (same probability or no "
3287 "compatible reaching):");
3288 curr_info.dump (dump_file, " ");
3289 }
3290 src_block_info.set_empty_info ();
3291 src_block_info.probability
3292 = profile_probability::uninitialized ();
3293 /* See PR113696, we should reset immediate dominator to
3294 empty since we may uplift ineffective vsetvl which
3295 locate at low probability block. */
3296 basic_block dom
3297 = get_immediate_dominator (CDI_DOMINATORS, eg->src);
3298 auto &dom_block_info = get_block_info (dom);
3299 if (dom_block_info.has_info ()
3300 && !m_dem.compatible_p (
3301 dom_block_info.get_exit_info (), curr_info))
3302 {
3303 dom_block_info.set_empty_info ();
3304 dom_block_info.probability
3305 = profile_probability::uninitialized ();
3306 if (dump_file && (dump_flags & TDF_DETAILS))
3307 {
3308 fprintf (dump_file,
3309 " Reset dominator bb %u:",
3310 dom->index);
3311 prev_info.dump (dump_file, " ");
3312 fprintf (dump_file,
3313 " due to (same probability or no "
3314 "compatible reaching):");
3315 curr_info.dump (dump_file, " ");
3316 }
3317 }
3318 changed = true;
3319 }
3320 /* Choose the one with higher probability. */
3321 else if (dest_block_info.probability
3322 > src_block_info.probability)
3323 {
3324 if (dump_file && (dump_flags & TDF_DETAILS))
3325 {
3326 fprintf (dump_file,
3327 " Change bb %u from:",
3328 eg->src->index);
3329 prev_info.dump (dump_file, " ");
3330 fprintf (dump_file,
3331 " to (higher probability):");
3332 curr_info.dump (dump_file, " ");
3333 }
3334 src_block_info.set_info (curr_info);
3335 src_block_info.probability = dest_block_info.probability;
3336 changed = true;
3337 }
3338 }
3339 }
3340 else
3341 {
3342 vsetvl_info &prev_info = src_block_info.get_exit_info ();
3343 if (!prev_info.valid_p ()
3344 || m_dem.available_p (prev_info, curr_info)
3345 || !m_dem.compatible_p (prev_info, curr_info))
3346 continue;
3347
3348 if (dump_file && (dump_flags & TDF_DETAILS))
3349 {
3350 fprintf (dump_file, " Fuse curr info since prev info "
3351 "compatible with it:\n");
3352 fprintf (dump_file, " prev_info: ");
3353 prev_info.dump (dump_file, " ");
3354 fprintf (dump_file, " curr_info: ");
3355 curr_info.dump (dump_file, " ");
3356 }
3357 m_dem.merge (prev_info, curr_info);
3358 if (dump_file && (dump_flags & TDF_DETAILS))
3359 {
3360 fprintf (dump_file, " prev_info after fused: ");
3361 prev_info.dump (dump_file, " ");
3362 fprintf (dump_file, "\n");
3363 }
3364 changed = true;
3365 }
3366 }
3367 }
3368
3369 if (dump_file && (dump_flags & TDF_DETAILS))
3370 {
3371 fprintf (dump_file, "\n");
3372 }
3373
3374 sbitmap_vector_free (antin);
3375 sbitmap_vector_free (antout);
3376 sbitmap_vector_free (earliest);
3377 free_edge_list (m_edges);
3378
3379 return changed;
3380 }
3381
3382 void
3383 pre_vsetvl::pre_global_vsetvl_info ()
3384 {
3385 compute_vsetvl_def_data ();
3386 compute_lcm_local_properties ();
3387
3388 unsigned num_exprs = m_exprs.length ();
3389 m_edges = pre_edge_lcm_avs (num_exprs, m_transp, m_avloc, m_antloc, m_kill,
3390 m_avin, m_avout, &m_insert, &m_del);
3391 unsigned num_edges = NUM_EDGES (m_edges);
3392
3393 if (dump_file && (dump_flags & TDF_DETAILS))
3394 {
3395 fprintf (dump_file, "\n Compute LCM insert and delete data:\n\n");
3396 fprintf (dump_file, " Expression List (%u):\n", num_exprs);
3397 for (unsigned i = 0; i < num_exprs; i++)
3398 {
3399 const auto &info = *m_exprs[i];
3400 fprintf (dump_file, " Expr[%u]: ", i);
3401 info.dump (dump_file, " ");
3402 }
3403 fprintf (dump_file, "\n bitmap data:\n");
3404 for (const bb_info *bb : crtl->ssa->bbs ())
3405 {
3406 unsigned i = bb->index ();
3407 fprintf (dump_file, " BB %u:\n", i);
3408 fprintf (dump_file, " avloc: ");
3409 dump_bitmap_file (dump_file, m_avloc[i]);
3410 fprintf (dump_file, " kill: ");
3411 dump_bitmap_file (dump_file, m_kill[i]);
3412 fprintf (dump_file, " antloc: ");
3413 dump_bitmap_file (dump_file, m_antloc[i]);
3414 fprintf (dump_file, " transp: ");
3415 dump_bitmap_file (dump_file, m_transp[i]);
3416
3417 fprintf (dump_file, " avin: ");
3418 dump_bitmap_file (dump_file, m_avin[i]);
3419 fprintf (dump_file, " avout: ");
3420 dump_bitmap_file (dump_file, m_avout[i]);
3421 fprintf (dump_file, " del: ");
3422 dump_bitmap_file (dump_file, m_del[i]);
3423 }
3424 fprintf (dump_file, "\n");
3425 fprintf (dump_file, " insert:\n");
3426 for (unsigned ed = 0; ed < num_edges; ed++)
3427 {
3428 edge eg = INDEX_EDGE (m_edges, ed);
3429
3430 if (bitmap_empty_p (m_insert[ed]))
3431 continue;
3432 fprintf (dump_file, " Edge(bb %u -> bb %u): ", eg->src->index,
3433 eg->dest->index);
3434 dump_bitmap_file (dump_file, m_insert[ed]);
3435 }
3436 }
3437
3438 /* Remove vsetvl infos as LCM suggest */
3439 for (const bb_info *bb : crtl->ssa->bbs ())
3440 {
3441 sbitmap d = m_del[bb->index ()];
3442 if (bitmap_count_bits (d) == 0)
3443 continue;
3444 gcc_assert (bitmap_count_bits (d) == 1);
3445 unsigned expr_index = bitmap_first_set_bit (d);
3446 vsetvl_info &info = *m_exprs[expr_index];
3447 gcc_assert (info.valid_p ());
3448 gcc_assert (info.get_bb () == bb);
3449 const vsetvl_block_info &block_info = get_block_info (info.get_bb ());
3450 gcc_assert (block_info.get_entry_info () == info);
3451 info.set_delete ();
3452 if (dump_file && (dump_flags & TDF_DETAILS))
3453 {
3454 fprintf (dump_file,
3455 "\nLCM deleting vsetvl of block %d, it has predecessors: \n",
3456 bb->index ());
3457 hash_set<basic_block> all_preds
3458 = get_all_predecessors (bb->cfg_bb ());
3459 int i = 0;
3460 for (const auto pred : all_preds)
3461 {
3462 fprintf (dump_file, "%d ", pred->index);
3463 i++;
3464 if (i % 32 == 0)
3465 fprintf (dump_file, "\n");
3466 }
3467 fprintf (dump_file, "\n");
3468 }
3469 }
3470
3471 /* Remove vsetvl infos if all predecessors are available to the block. */
3472 for (const bb_info *bb : crtl->ssa->bbs ())
3473 {
3474 vsetvl_block_info &block_info = get_block_info (bb);
3475 if (block_info.empty_p ())
3476 continue;
3477 vsetvl_info &curr_info = block_info.get_entry_info ();
3478 if (!curr_info.valid_p ())
3479 continue;
3480
3481 unsigned int expr_index;
3482 sbitmap_iterator sbi;
3483 gcc_assert (
3484 !bitmap_empty_p (m_vsetvl_def_in[curr_info.get_bb ()->index ()]));
3485 bool full_available = true;
3486 EXECUTE_IF_SET_IN_BITMAP (m_vsetvl_def_in[bb->index ()], 0, expr_index,
3487 sbi)
3488 {
3489 vsetvl_info &prev_info = *m_vsetvl_def_exprs[expr_index];
3490 if (!prev_info.valid_p ()
3491 || !m_dem.available_p (prev_info, curr_info))
3492 {
3493 full_available = false;
3494 break;
3495 }
3496 }
3497 if (full_available)
3498 curr_info.set_delete ();
3499 }
3500
3501 for (const bb_info *bb : crtl->ssa->bbs ())
3502 {
3503 vsetvl_block_info &block_info = get_block_info (bb);
3504 if (block_info.empty_p ())
3505 continue;
3506 vsetvl_info &curr_info = block_info.get_entry_info ();
3507 if (curr_info.delete_p ())
3508 {
3509 if (block_info.local_infos.is_empty ())
3510 continue;
3511 curr_info = block_info.local_infos[0];
3512 }
3513 if (curr_info.valid_p () && !curr_info.vl_used_by_non_rvv_insn_p ()
3514 && preds_all_same_avl_and_ratio_p (curr_info))
3515 curr_info.set_change_vtype_only ();
3516
3517 vsetvl_info prev_info = vsetvl_info ();
3518 prev_info.set_empty ();
3519 for (auto &curr_info : block_info.local_infos)
3520 {
3521 if (prev_info.valid_p () && curr_info.valid_p ()
3522 && m_dem.avl_available_p (prev_info, curr_info)
3523 && prev_info.get_ratio () == curr_info.get_ratio ())
3524 curr_info.set_change_vtype_only ();
3525 prev_info = curr_info;
3526 }
3527 }
3528 }
3529
3530 void
3531 pre_vsetvl::emit_vsetvl ()
3532 {
3533 bool need_commit = false;
3534
3535 /* Fake edge is created by connect infinite loops to exit function.
3536 We should commit vsetvl edge after fake edges removes, otherwise,
3537 it will cause ICE. */
3538 remove_fake_exit_edges ();
3539 for (const bb_info *bb : crtl->ssa->bbs ())
3540 {
3541 for (const auto &curr_info : get_block_info (bb).local_infos)
3542 {
3543 insn_info *insn = curr_info.get_insn ();
3544 if (curr_info.delete_p ())
3545 {
3546 if (vsetvl_insn_p (insn->rtl ()))
3547 remove_vsetvl_insn (curr_info.get_insn ()->rtl ());
3548 continue;
3549 }
3550 else if (curr_info.valid_p ())
3551 {
3552 if (vsetvl_insn_p (insn->rtl ()))
3553 {
3554 const vsetvl_info temp = vsetvl_info (insn);
3555 if (!(curr_info == temp))
3556 {
3557 if (dump_file)
3558 {
3559 fprintf (dump_file, "\n Change vsetvl info from: ");
3560 temp.dump (dump_file, " ");
3561 fprintf (dump_file, " to: ");
3562 curr_info.dump (dump_file, " ");
3563 }
3564 change_vsetvl_insn (curr_info);
3565 }
3566 }
3567 else
3568 {
3569 if (dump_file)
3570 {
3571 fprintf (dump_file,
3572 "\n Insert vsetvl info before insn %d: ",
3573 insn->uid ());
3574 curr_info.dump (dump_file, " ");
3575 }
3576 insert_vsetvl_insn (EMIT_BEFORE, curr_info);
3577 }
3578 }
3579 }
3580 }
3581
3582 for (const vsetvl_info &item : m_delete_list)
3583 {
3584 gcc_assert (vsetvl_insn_p (item.get_insn ()->rtl ()));
3585 remove_vsetvl_insn (item.get_insn ()->rtl ());
3586 }
3587
3588 /* Insert vsetvl info that was not deleted after lift up. */
3589 for (const bb_info *bb : crtl->ssa->bbs ())
3590 {
3591 const vsetvl_block_info &block_info = get_block_info (bb);
3592 if (!block_info.has_info ())
3593 continue;
3594
3595 const vsetvl_info &footer_info = block_info.get_exit_info ();
3596
3597 if (footer_info.delete_p ())
3598 continue;
3599
3600 edge eg;
3601 edge_iterator eg_iterator;
3602 FOR_EACH_EDGE (eg, eg_iterator, bb->cfg_bb ()->succs)
3603 {
3604 gcc_assert (!(eg->flags & EDGE_ABNORMAL));
3605 if (dump_file)
3606 {
3607 fprintf (
3608 dump_file,
3609 "\n Insert missed vsetvl info at edge(bb %u -> bb %u): ",
3610 eg->src->index, eg->dest->index);
3611 footer_info.dump (dump_file, " ");
3612 }
3613 start_sequence ();
3614 insert_vsetvl_insn (EMIT_DIRECT, footer_info);
3615 rtx_insn *rinsn = get_insns ();
3616 end_sequence ();
3617 default_rtl_profile ();
3618 insert_insn_on_edge (rinsn, eg);
3619 need_commit = true;
3620 }
3621 }
3622
3623 /* m_insert vsetvl as LCM suggest. */
3624 for (int ed = 0; ed < NUM_EDGES (m_edges); ed++)
3625 {
3626 edge eg = INDEX_EDGE (m_edges, ed);
3627 sbitmap i = m_insert[ed];
3628 if (bitmap_count_bits (i) != 1)
3629 /* For code with infinite loop (e.g. pr61634.c), The data flow is
3630 completely wrong. */
3631 continue;
3632
3633 unsigned expr_index = bitmap_first_set_bit (i);
3634 const vsetvl_info &info = *m_exprs[expr_index];
3635 gcc_assert (info.valid_p ());
3636 if (dump_file)
3637 {
3638 fprintf (dump_file,
3639 "\n Insert vsetvl info at edge(bb %u -> bb %u): ",
3640 eg->src->index, eg->dest->index);
3641 info.dump (dump_file, " ");
3642 }
3643 rtl_profile_for_edge (eg);
3644 start_sequence ();
3645
3646 insert_vsetvl_insn (EMIT_DIRECT, info);
3647 rtx_insn *rinsn = get_insns ();
3648 end_sequence ();
3649 default_rtl_profile ();
3650
3651 /* We should not get an abnormal edge here. */
3652 gcc_assert (!(eg->flags & EDGE_ABNORMAL));
3653 need_commit = true;
3654 insert_insn_on_edge (rinsn, eg);
3655 }
3656
3657 if (need_commit)
3658 commit_edge_insertions ();
3659 }
3660
3661 void
3662 pre_vsetvl::cleanup ()
3663 {
3664 remove_avl_operand ();
3665 remove_unused_dest_operand ();
3666 remove_vsetvl_pre_insns ();
3667 }
3668
3669 void
3670 pre_vsetvl::remove_avl_operand ()
3671 {
3672 basic_block cfg_bb;
3673 rtx_insn *rinsn;
3674 FOR_ALL_BB_FN (cfg_bb, cfun)
3675 FOR_BB_INSNS (cfg_bb, rinsn)
3676 if (NONDEBUG_INSN_P (rinsn) && has_vl_op (rinsn)
3677 && REG_P (get_vl (rinsn)))
3678 {
3679 rtx avl = get_vl (rinsn);
3680 if (count_regno_occurrences (rinsn, REGNO (avl)) == 1)
3681 {
3682 rtx new_pat;
3683 if (fault_first_load_p (rinsn))
3684 new_pat
3685 = simplify_replace_rtx (PATTERN (rinsn), avl, const0_rtx);
3686 else
3687 {
3688 rtx set = single_set (rinsn);
3689 rtx src
3690 = simplify_replace_rtx (SET_SRC (set), avl, const0_rtx);
3691 new_pat = gen_rtx_SET (SET_DEST (set), src);
3692 }
3693 if (dump_file)
3694 {
3695 fprintf (dump_file, " Cleanup insn %u's avl operand:\n",
3696 INSN_UID (rinsn));
3697 print_rtl_single (dump_file, rinsn);
3698 }
3699 validate_change_or_fail (rinsn, &PATTERN (rinsn), new_pat, false);
3700 }
3701 }
3702 }
3703
3704 void
3705 pre_vsetvl::remove_unused_dest_operand ()
3706 {
3707 df_analyze ();
3708 basic_block cfg_bb;
3709 rtx_insn *rinsn;
3710 FOR_ALL_BB_FN (cfg_bb, cfun)
3711 FOR_BB_INSNS (cfg_bb, rinsn)
3712 if (NONDEBUG_INSN_P (rinsn) && vsetvl_insn_p (rinsn))
3713 {
3714 rtx vl = get_vl (rinsn);
3715 vsetvl_info info = vsetvl_info (rinsn);
3716 if (has_no_uses (cfg_bb, rinsn, REGNO (vl)))
3717 if (!info.has_vlmax_avl ())
3718 {
3719 rtx new_pat = info.get_vsetvl_pat (true);
3720 if (dump_file)
3721 {
3722 fprintf (dump_file,
3723 " Remove vsetvl insn %u's dest(vl) operand since "
3724 "it unused:\n",
3725 INSN_UID (rinsn));
3726 print_rtl_single (dump_file, rinsn);
3727 }
3728 validate_change_or_fail (rinsn, &PATTERN (rinsn), new_pat,
3729 false);
3730 }
3731 }
3732 }
3733
3734 /* Remove all bogus vsetvl_pre instructions. */
3735 void
3736 pre_vsetvl::remove_vsetvl_pre_insns ()
3737 {
3738 basic_block cfg_bb;
3739 rtx_insn *rinsn;
3740 FOR_ALL_BB_FN (cfg_bb, cfun)
3741 FOR_BB_INSNS (cfg_bb, rinsn)
3742 if (NONDEBUG_INSN_P (rinsn) && vsetvl_pre_insn_p (rinsn))
3743 {
3744 if (dump_file)
3745 {
3746 fprintf (dump_file, " Eliminate vsetvl_pre insn %d:\n",
3747 INSN_UID (rinsn));
3748 print_rtl_single (dump_file, rinsn);
3749 }
3750 remove_vsetvl_insn (rinsn);
3751 }
3752 }
3753
3754 const pass_data pass_data_vsetvl = {
3755 RTL_PASS, /* type */
3756 "vsetvl", /* name */
3757 OPTGROUP_NONE, /* optinfo_flags */
3758 TV_MACH_DEP, /* tv_id */
3759 0, /* properties_required */
3760 0, /* properties_provided */
3761 0, /* properties_destroyed */
3762 0, /* todo_flags_start */
3763 0, /* todo_flags_finish */
3764 };
3765
3766 class pass_vsetvl : public rtl_opt_pass
3767 {
3768 private:
3769 void simple_vsetvl ();
3770 void lazy_vsetvl ();
3771
3772 public:
3773 pass_vsetvl (gcc::context *ctxt) : rtl_opt_pass (pass_data_vsetvl, ctxt) {}
3774
3775 /* opt_pass methods: */
3776 virtual bool gate (function *) final override { return TARGET_VECTOR; }
3777 virtual unsigned int execute (function *) final override;
3778 }; // class pass_vsetvl
3779
3780 void
3781 pass_vsetvl::simple_vsetvl ()
3782 {
3783 if (dump_file)
3784 fprintf (dump_file, "\nEntering Simple VSETVL PASS\n");
3785
3786 basic_block cfg_bb;
3787 rtx_insn *rinsn;
3788 FOR_ALL_BB_FN (cfg_bb, cfun)
3789 {
3790 FOR_BB_INSNS (cfg_bb, rinsn)
3791 {
3792 if (!NONDEBUG_INSN_P (rinsn))
3793 continue;
3794 if (has_vtype_op (rinsn))
3795 {
3796 const auto &info = vsetvl_info (rinsn);
3797 rtx pat = info.get_vsetvl_pat ();
3798 emit_insn_before (pat, rinsn);
3799 if (dump_file)
3800 {
3801 fprintf (dump_file, " Insert vsetvl insn before insn %d:\n",
3802 INSN_UID (rinsn));
3803 print_rtl_single (dump_file, PREV_INSN (rinsn));
3804 }
3805 }
3806 }
3807 }
3808
3809 if (dump_file)
3810 fprintf (dump_file, "\nEmit missing read_vl()s for fault-only-first "
3811 "loads\n");
3812 emit_fof_read_vls ();
3813 }
3814
3815 /* Lazy vsetvl insertion for optimize > 0. */
3816 void
3817 pass_vsetvl::lazy_vsetvl ()
3818 {
3819 if (dump_file)
3820 fprintf (dump_file, "\nEntering Lazy VSETVL PASS\n\n");
3821
3822 pre_vsetvl pre = pre_vsetvl ();
3823
3824 if (dump_file)
3825 fprintf (dump_file, "\nPhase 1: Fuse local vsetvl infos.\n\n");
3826 pre.fuse_local_vsetvl_info ();
3827 if (dump_file && (dump_flags & TDF_DETAILS))
3828 pre.dump (dump_file, "phase 1");
3829
3830 /* Phase 2: Fuse header and footer vsetvl infos between basic blocks. */
3831 if (dump_file)
3832 fprintf (dump_file, "\nPhase 2: Lift up vsetvl info.\n\n");
3833 if (vsetvl_strategy != VSETVL_OPT_NO_FUSION)
3834 {
3835 bool changed = true;
3836 int fused_count = 0;
3837 do
3838 {
3839 if (dump_file)
3840 fprintf (dump_file, " Try lift up %d.\n\n", fused_count);
3841 changed = pre.earliest_fuse_vsetvl_info (fused_count);
3842 fused_count += 1;
3843 } while (changed);
3844 }
3845 if (dump_file && (dump_flags & TDF_DETAILS))
3846 pre.dump (dump_file, "phase 2");
3847
3848 /* Phase 3: Reducing redundant vsetvl infos using LCM. */
3849 if (dump_file)
3850 fprintf (dump_file, "\nPhase 3: Reduce global vsetvl infos.\n\n");
3851 pre.pre_global_vsetvl_info ();
3852 if (dump_file && (dump_flags & TDF_DETAILS))
3853 pre.dump (dump_file, "phase 3");
3854
3855 /* Phase 4: Insert, modify and remove vsetvl insns. */
3856 if (dump_file)
3857 fprintf (dump_file,
3858 "\nPhase 4: Insert, modify and remove vsetvl insns.\n\n");
3859 pre.emit_vsetvl ();
3860
3861 /* Phase 4b: Emit read_vl for fault-only-first loads with VL output
3862 register. */
3863 if (dump_file)
3864 fprintf (dump_file, "\nPhase 4b: Emit missing read_vl()s for "
3865 "fault-only-first loads\n");
3866 emit_fof_read_vls ();
3867
3868 /* Phase 5: Cleanup */
3869 if (dump_file)
3870 fprintf (dump_file, "\nPhase 5: Cleanup\n\n");
3871 pre.cleanup ();
3872
3873 pre.finish ();
3874 }
3875
3876 /* Main entry point for this pass. */
3877 unsigned int
3878 pass_vsetvl::execute (function *)
3879 {
3880 if (n_basic_blocks_for_fn (cfun) <= 0)
3881 return 0;
3882
3883 /* The RVV instruction may change after split which is not a stable
3884 instruction. We need to split it here to avoid potential issue
3885 since the VSETVL PASS is insert before split PASS. */
3886 split_all_insns ();
3887
3888 /* Early return for there is no vector instructions. */
3889 if (!has_vector_insn (cfun))
3890 return 0;
3891
3892 if (!optimize || vsetvl_strategy == VSETVL_SIMPLE)
3893 simple_vsetvl ();
3894 else
3895 lazy_vsetvl ();
3896
3897 return 0;
3898 }
3899
3900 rtl_opt_pass *
3901 make_pass_vsetvl (gcc::context *ctxt)
3902 {
3903 return new pass_vsetvl (ctxt);
3904 }