Debian Codegraph @

gcc/config/aarch64/aarch64-early-ra.cc

raw
1 // Early register allocation pass.
2 // Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation; either version 3, or (at your option) any later
9 // version.
10 //
11 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // This pass implements a simple form of early register allocation.
21 // It is restricted to FP/SIMD registers, and it only allocates
22 // a region of FP/SIMD usage if it can do so without any spilling.
23 // It punts on anything too complicated, leaving it to the real
24 // register allocator.
25 //
26 // There are two main purposes:
27 //
28 // (1) The pass runs before scheduling. It therefore has a chance to
29 // bag a spill-free allocation, if there is one, before scheduling
30 // moves things around.
31 //
32 // (2) The pass can make use of strided register operations, such as the
33 // strided forms of LD1 and ST1 in SME2.
34 //
35 // The allocator works at the level of individual FPRs, rather than whole
36 // pseudo registers. It is mostly intended to help optimize ACLE code.
37 //
38 // The pass is very simplistic. There are many things that could be improved.
39 #define IN_TARGET_CODE 1
40
41 #define INCLUDE_ALGORITHM
42 #define INCLUDE_FUNCTIONAL
43 #define INCLUDE_ARRAY
44 #include "config.h"
45 #include "system.h"
46 #include "coretypes.h"
47 #include "backend.h"
48 #include "rtl.h"
49 #include "df.h"
50 #include "rtl-ssa.h"
51 #include "tree-pass.h"
52 #include "target.h"
53 #include "expr.h"
54 #include "cfgrtl.h"
55 #include "print-rtl.h"
56 #include "insn-attr.h"
57 #include "insn-opinit.h"
58 #include "reload.h"
59
60 template<typename T>
61 class simple_iterator : public wrapper_iterator<T>
62 {
63 public:
64 using wrapper_iterator<T>::wrapper_iterator;
65
66 simple_iterator &operator-- () { --this->m_contents; return *this; }
67 simple_iterator operator-- (int) { return this->m_contents--; }
68 simple_iterator &operator++ () { ++this->m_contents; return *this; }
69 simple_iterator operator++ (int) { return this->m_contents++; }
70 };
71
72 using namespace rtl_ssa;
73
74 namespace {
75 const pass_data pass_data_early_ra =
76 {
77 RTL_PASS, // type
78 "early_ra", // name
79 OPTGROUP_NONE, // optinfo_flags
80 TV_NONE, // tv_id
81 0, // properties_required
82 0, // properties_provided
83 0, // properties_destroyed
84 0, // todo_flags_start
85 TODO_df_finish, // todo_flags_finish
86 };
87
88 using allocno_iterator = simple_iterator<unsigned int>;
89
90 // Class that represents one run of the pass.
91 class early_ra
92 {
93 public:
94 early_ra (function *fn);
95 ~early_ra ();
96 void execute ();
97
98 private:
99 // Whether to test only things that are required for correctness,
100 // or whether to take optimization heuristics into account as well.
101 enum test_strictness { CORRECTNESS_ONLY, ALL_REASONS };
102
103 static_assert (MAX_RECOG_OPERANDS <= 32, "Operand mask is 32 bits");
104 using operand_mask = uint32_t;
105
106 // Points in the function are represented using "program points".
107 // The program points are allocated in reverse order, with smaller
108 // numbers indicating later points. These special values indicate
109 // the start and end of a region.
110 static constexpr unsigned int START_OF_REGION = ~0U;
111 static constexpr unsigned int END_OF_REGION = 0U;
112
113 // An invalid allocno index, used to represent no allocno.
114 static constexpr unsigned int INVALID_ALLOCNO = ~0U;
115
116 // Enumerates the single FPR sizes that matter for register allocation.
117 // Anything smaller than 64 bits is treated as FPR_D.
118 enum fpr_size_info
119 {
120 FPR_D,
121 FPR_Q,
122 FPR_Z
123 };
124
125 // A live range for an FPR, containing program points [START_POINT,
126 // END_POINT]. If ALLOCNO is not INVALID_ALLOCNO, the FPR is known
127 // to be equal to ALLOCNO for the duration of the live range.
128 struct fpr_range_info
129 {
130 unsigned int start_point;
131 unsigned int end_point;
132 unsigned int allocno;
133 };
134
135 // Flags used in pseudo_reg_info.
136 //
137 // Whether the pseudo register occurs in one instruction alternative that
138 // matches (respectively) V0-V7, V0-V15, V0-V31 or a non-FP register.
139 static constexpr unsigned int ALLOWS_FPR8 = 1U << 0;
140 static constexpr unsigned int ALLOWS_FPR16 = 1U << 1;
141 static constexpr unsigned int ALLOWS_FPR32 = 1U << 2;
142 static constexpr unsigned int ALLOWS_NONFPR = 1U << 3;
143 //
144 // Likewise whether the register occurs in an instruction that requires
145 // the associated register type.
146 static constexpr unsigned int NEEDS_FPR8 = 1U << 4;
147 static constexpr unsigned int NEEDS_FPR16 = 1U << 5;
148 static constexpr unsigned int NEEDS_FPR32 = 1U << 6;
149 static constexpr unsigned int NEEDS_NONFPR = 1U << 7;
150 //
151 // Whether the pseudo register is copied to or from a hard FP register.
152 static constexpr unsigned int HAS_FPR_COPY = 1U << 8;
153 //
154 // Whether the pseudo register is copied to or from a hard non-FP register.
155 static constexpr unsigned int HAS_NONFPR_COPY = 1U << 9;
156 //
157 // Whether the pseudo register is used as a multi-register vector operand
158 // to an instruction that supports strided accesses, and whether it is used
159 // as a multi-register vector operand in some other non-move instruction.
160 static constexpr unsigned int HAS_FLEXIBLE_STRIDE = 1U << 10;
161 static constexpr unsigned int HAS_FIXED_STRIDE = 1U << 11;
162 //
163 // Whether we have decided not to allocate the pseudo register.
164 static constexpr unsigned int IGNORE_REG = 1U << 12;
165
166 // Flags that should be propagated across moves between pseudo registers.
167 static constexpr unsigned int PSEUDO_COPY_FLAGS = ~(HAS_FLEXIBLE_STRIDE
168 | HAS_FIXED_STRIDE);
169
170 // Information about a copy between two registers.
171 struct reg_copy_info
172 {
173 // The two registers, in order.
174 unsigned int regnos[2];
175
176 // Index I gives the index of the next reg_copy_info involving REGNOS[I],
177 // or 0 if none.
178 unsigned int next_copies[2];
179 };
180
181 // Information about a pseudo register.
182 struct pseudo_reg_info
183 {
184 // Flags describing how the register is used, defined above.
185 unsigned int flags : 16;
186
187 // The mode of the pseudo register, cached for convenience.
188 machine_mode mode : 16;
189
190 // The index of the first copy, or 0 if none.
191 unsigned int first_copy;
192 };
193
194 // Information about a group of allocnos that have a fixed offset
195 // relative to each other. The allocnos in each group must be allocated
196 // together.
197 //
198 // Allocnos that can share the same hard register are eventually
199 // chained together. These chains represent edges on a graph of
200 // allocnos, such that two allocnos joined by an edge use the same FPR.
201 // These chains are formed between individual allocnos rather than
202 // whole groups, although the system is required to be self-consistent.
203 // Each clique in the graph has at least one "full-width" allocno group
204 // that has one allocno for every FPR that needs to be allocated to
205 // the clique.
206 //
207 // One group of allocnos is chosen as the "color representative" of
208 // each clique in the graph. This group will be a full-width group.
209 struct allocno_info;
210 struct allocno_group_info
211 {
212 array_slice<unsigned int> chain_heads ();
213 array_slice<allocno_info> allocnos ();
214 allocno_group_info *color_rep ();
215 allocno_info *allocno (unsigned int);
216
217 // The color representative of the containing clique.
218 allocno_group_info *m_color_rep;
219
220 // The pseudo register associated with this allocno, or INVALID_REGNUM
221 // if none.
222 unsigned int regno;
223
224 // The offset of the first allocno (and thus this group) from the start
225 // of color_rep.
226 unsigned int color_rep_offset : 8;
227
228 // The number of allocnos in the group, and thus the number of FPRs
229 // that need to be allocated.
230 unsigned int size : 8;
231
232 // The gap between FPRs in the group. This is normally 1, but can be
233 // higher if we've decided to use strided multi-register accesses.
234 unsigned int stride : 4;
235
236 // Used temporarily while deciding which allocnos should have non-unit
237 // strides; see find_strided_accesses for details.
238 int consecutive_pref : 4;
239 int strided_polarity : 2;
240
241 // The largest size of FPR needed by references to the allocno group.
242 fpr_size_info fpr_size : 2;
243
244 // True if all non-move accesses can be converted to strided form.
245 unsigned int has_flexible_stride : 1;
246
247 // True if we've assigned a color index to this group.
248 unsigned int has_color : 1;
249
250 // The mask of FPRs that would make valid choices for the first allocno,
251 // taking the requirements of all the allocnos in the group into account.
252 unsigned int fpr_candidates;
253
254 // The index of the color that has been assigned to the containing clique.
255 unsigned int color;
256 };
257
258 // Represents a single FPR-sized quantity that needs to be allocated.
259 // Each allocno is identified by index (for compactness).
260 //
261 // Quantities that span multiple FPRs are assigned groups of consecutive
262 // allocnos. Quantities that occupy a single FPR are assigned their own
263 // group.
264 struct allocno_info
265 {
266 allocno_group_info *group ();
267 bool is_shared ();
268 bool is_equiv_to (unsigned int);
269
270 // The allocno's unique identifier.
271 unsigned int id;
272
273 // The offset of this allocno into the containing group.
274 unsigned int offset : 8;
275
276 // The number of allocnos in the containing group.
277 unsigned int group_size : 8;
278
279 // If the allocno has an affinity with at least one hard register
280 // (so that choosing that hard register would avoid a copy), this is
281 // the number of one such hard register, otherwise it is
282 // FIRST_PSEUDO_REGISTER.
283 unsigned int hard_regno : 8;
284
285 // Set to 1 if the allocno has a single definition or 2 if it has more.
286 unsigned int num_defs : 2;
287
288 // True if, at START_POINT, another allocno is copied to this one.
289 // See callers of record_copy for what counts as a copy.
290 unsigned int is_copy_dest : 1;
291
292 // True if, at START_POINT, another allocno is copied to this one,
293 // and if the allocnos at both ends of the copy chain have an affinity
294 // with the same hard register.
295 unsigned int is_strong_copy_dest : 1;
296
297 // True if, at END_POINT, this allocno is copied to another one,
298 // and both allocnos have an affinity with the same hard register.
299 unsigned int is_strong_copy_src : 1;
300
301 // True if the allocno is subject to an earlyclobber at END_POINT,
302 // so that it cannot be tied to the destination of the instruction.
303 unsigned int is_earlyclobbered : 1;
304
305 // True if this allocno is known to be equivalent to related_allocno
306 // for the whole of this allocno's lifetime.
307 unsigned int is_equiv : 1;
308
309 // The inclusive range of program points spanned by the allocno.
310 // START_POINT >= END_POINT.
311 unsigned int start_point;
312 unsigned int end_point;
313
314 // If, at END_POINT, this allocno is copied to another allocno, this
315 // is the index of that allocno, otherwise it is INVALID_ALLOCNO.
316 // See callers of record_copy for what counts as a copy.
317 unsigned int copy_dest;
318
319 // If this field is not INVALID_ALLOCNO, it indicates one of two things:
320 //
321 // - if is_equiv, this allocno is equivalent to related_allocno for
322 // the whole of this allocno's lifetime.
323 //
324 // - if !is_equiv, this allocno's live range is a subrange of
325 // related_allocno's and we have committed to making this allocno
326 // share whatever register related_allocno uses.
327 unsigned int related_allocno;
328
329 union
330 {
331 // The program point at which the allocno was last defined,
332 // or START_OF_REGION if none. This is only used temporarily
333 // while recording allocnos; after that, chain_next below is
334 // used instead.
335 unsigned int last_def_point;
336
337 // The next chained allocno in program order (i.e. at lower program
338 // points), or INVALID_ALLOCNO if none.
339 unsigned int chain_next;
340 };
341
342 union
343 {
344 // The program point before start_point at which the allocno was
345 // last used, or END_OF_REGION if none. This is only used temporarily
346 // while recording allocnos; after that, chain_prev below is used
347 // instead.
348 unsigned int last_use_point;
349
350 // The previous chained allocno in program order (i.e. at higher
351 // program points), or INVALID_ALLOCNO if none.
352 unsigned int chain_prev;
353 };
354 };
355
356 // Information about a full allocno group or a subgroup of it.
357 // The subgroup can be empty to indicate "none".
358 struct allocno_subgroup
359 {
360 array_slice<allocno_info> allocnos ();
361 allocno_info *allocno (unsigned int);
362
363 // True if a subgroup is present.
364 operator bool () const { return count; }
365
366 // The containing group.
367 allocno_group_info *group;
368
369 // The offset of the subgroup from the start of GROUP.
370 unsigned int start;
371
372 // The number of allocnos in the subgroup.
373 unsigned int count;
374 };
375
376 // Represents information about a copy between an allocno and an FPR.
377 // This establishes an affinity between the allocno and the FPR.
378 struct allocno_copy_info
379 {
380 // The allocno involved in the copy.
381 unsigned int allocno;
382
383 // The FPR involved in the copy, relative to V0_REGNUM.
384 unsigned int fpr : 16;
385
386 // A measure of how strong the affinity between the allocno and FPR is.
387 unsigned int weight : 16;
388 };
389
390 // Information about a possible allocno chain.
391 struct chain_candidate_info
392 {
393 // The candidate target allocno.
394 allocno_info *allocno;
395
396 // A rating of the candidate (higher is better).
397 int score;
398 };
399
400 // Information about an allocno color.
401 struct color_info
402 {
403 // The color's unique identifier.
404 int id;
405
406 // The allocated hard register, when known.
407 unsigned int hard_regno;
408
409 // The clique's representative group.
410 allocno_group_info *group;
411
412 // The number of FPR preferences recorded in fpr_preferences.
413 unsigned int num_fpr_preferences;
414
415 // Weights in favor of choosing each FPR as the first register for GROUP.
416 int8_t fpr_preferences[32];
417 };
418
419 template<typename T, typename... Ts>
420 T *region_allocate (Ts...);
421
422 allocno_info *chain_prev (allocno_info *);
423 allocno_info *chain_next (allocno_info *);
424
425 void dump_pseudo_regs ();
426 void dump_fpr_ranges ();
427 void dump_copies ();
428 void dump_allocnos ();
429 void dump_colors ();
430
431 iterator_range<allocno_iterator> get_group_allocnos (unsigned int);
432
433 void preprocess_move (rtx, rtx);
434 void process_pseudo_reg_constraints (rtx_insn *);
435 void preprocess_insns ();
436
437 int fpr_preference (unsigned int);
438 void propagate_pseudo_reg_info ();
439
440 void choose_fpr_pseudos ();
441
442 void start_new_region ();
443
444 template<typename T>
445 void record_live_range_failure (T);
446 template<typename T>
447 void record_allocation_failure (T);
448
449 allocno_group_info *create_allocno_group (unsigned int, unsigned int);
450 allocno_subgroup get_allocno_subgroup (rtx);
451 void record_fpr_use (unsigned int);
452 void record_fpr_def (unsigned int);
453 void record_allocno_use (allocno_info *);
454 void record_allocno_def (allocno_info *);
455 allocno_info *find_related_start (allocno_info *, allocno_info *, bool);
456 void accumulate_defs (allocno_info *, allocno_info *);
457 void record_copy (rtx, rtx, bool = false);
458 void record_constraints (rtx_insn *);
459 void record_artificial_refs (unsigned int);
460 void record_insn_defs (rtx_insn *);
461 void record_insn_call (rtx_call_insn *);
462 void record_insn_uses (rtx_insn *);
463
464 bool consider_strong_copy_src_chain (allocno_info *);
465 int strided_polarity_pref (allocno_info *, allocno_info *);
466 void find_strided_accesses ();
467
468 template<unsigned int allocno_info::*field>
469 static int cmp_increasing (const void *, const void *);
470 bool is_chain_candidate (allocno_info *, allocno_info *, test_strictness);
471 int rate_chain (allocno_info *, allocno_info *);
472 static int cmp_chain_candidates (const void *, const void *);
473 void chain_allocnos (unsigned int &, unsigned int &);
474 void merge_fpr_info (allocno_group_info *, allocno_group_info *,
475 unsigned int);
476 void set_single_color_rep (allocno_info *, allocno_group_info *,
477 unsigned int);
478 void set_color_rep (allocno_group_info *, allocno_group_info *,
479 unsigned int);
480 bool try_to_chain_allocnos (allocno_info *, allocno_info *);
481 void create_color (allocno_group_info *);
482 void form_chains ();
483
484 bool fpr_conflicts_with_allocno_p (unsigned int, allocno_info *);
485 bool call_in_range_p (unsigned int, unsigned int, unsigned int);
486 unsigned int partial_fpr_clobbers (unsigned int, fpr_size_info);
487
488 void process_copies ();
489
490 static int cmp_allocation_order (const void *, const void *);
491 void allocate_colors ();
492 allocno_info *find_independent_subchain (allocno_info *);
493 color_info *find_oldest_color (unsigned int, unsigned int);
494 void broaden_colors ();
495 void finalize_allocation ();
496
497 bool replace_regs (rtx_insn *, df_ref);
498 int try_enforce_constraints (rtx_insn *, vec<std::pair<int, int>> &);
499 void enforce_constraints (rtx_insn *);
500 bool maybe_convert_to_strided_access (rtx_insn *);
501 void apply_allocation ();
502
503 void process_region ();
504 bool is_dead_insn (rtx_insn *);
505 bool could_split_region_here ();
506 void process_block (basic_block, bool);
507 void process_blocks ();
508
509 // ----------------------------------------------------------------------
510
511 // The function we're operating on.
512 function *m_fn;
513
514 // Information about each pseudo register, indexed by REGNO.
515 auto_vec<pseudo_reg_info> m_pseudo_regs;
516
517 // All recorded register copies.
518 auto_vec<reg_copy_info> m_pseudo_reg_copies;
519
520 // The set of pseudos that we've decided to allocate an FPR to.
521 auto_bitmap m_fpr_pseudos;
522
523 // ----------------------------------------------------------------------
524
525 // An obstack for allocating information that is referenced by the member
526 // variables below.
527 obstack m_region_obstack;
528 void *m_region_alloc_start;
529
530 // ----------------------------------------------------------------------
531
532 // The basic block that we're currently processing.
533 basic_block m_current_bb;
534
535 // The lowest-numbered program point in the current basic block.
536 unsigned int m_current_bb_point;
537
538 // The program point that we're currently processing (described above).
539 unsigned int m_current_point;
540
541 // The set of allocnos that are currently live.
542 auto_bitmap m_live_allocnos;
543
544 // The set of FPRs that are currently live.
545 unsigned int m_live_fprs;
546
547 // A unique one-based identifier for the current region.
548 unsigned int m_current_region;
549
550 // The region in which each FPR was last used, or 0 if none.
551 unsigned int m_fpr_recency[32];
552
553 // ----------------------------------------------------------------------
554
555 // A mask of the FPRs that have already been allocated.
556 unsigned int m_allocated_fprs;
557
558 // A mask of the FPRs that must be at least partially preserved by the
559 // current function.
560 unsigned int m_call_preserved_fprs;
561
562 // True if we have so far been able to track the live ranges of individual
563 // allocnos.
564 bool m_accurate_live_ranges;
565
566 // True if we haven't yet failed to allocate the current region.
567 bool m_allocation_successful;
568
569 // A map from pseudo registers to the first allocno in their associated
570 // allocno groups.
571 hash_map<int_hash<unsigned int, INVALID_REGNUM>,
572 allocno_group_info *> m_regno_to_group;
573
574 // All recorded copies between allocnos and FPRs.
575 auto_vec<allocno_copy_info> m_allocno_copies;
576
577 // All allocnos, by index.
578 auto_vec<allocno_info *> m_allocnos;
579
580 // All allocnos, by increasing START_POINT.
581 auto_vec<allocno_info *> m_sorted_allocnos;
582
583 // Allocnos for which is_shared is true.
584 auto_vec<allocno_info *> m_shared_allocnos;
585
586 // All colors, by index.
587 auto_vec<color_info *> m_colors;
588
589 // The instruction ranges that make up the current region,
590 // as half-open ranges [LAST, FIRST).
591 auto_vec<std::pair<rtx_insn *, rtx_insn *>> m_insn_ranges;
592
593 // The live ranges of each FPR, in order of increasing program point.
594 auto_vec<fpr_range_info> m_fpr_ranges[32];
595
596 // For each function call id, a list of program points at which a call
597 // to such a function is made. Each list is in order of increasing
598 // program point.
599 auto_vec<unsigned int> m_call_points[NUM_ABI_IDS];
600
601 // A list of instructions that can be removed if allocation succeeds.
602 auto_vec<rtx_insn *> m_dead_insns;
603 };
604
605 // True if PAT is something that would typically be treated as a move.
606 static inline bool
607 is_move_set (rtx pat)
608 {
609 if (GET_CODE (pat) != SET)
610 return false;
611
612 rtx dest = SET_DEST (pat);
613 if (SUBREG_P (dest))
614 dest = SUBREG_REG (dest);
615 if (!OBJECT_P (dest))
616 return false;
617
618 rtx src = SET_SRC (pat);
619 if (SUBREG_P (src))
620 src = SUBREG_REG (src);
621 if (!OBJECT_P (src) && !CONSTANT_P (src))
622 return false;
623
624 return true;
625 }
626
627 // Return true if operand OP is likely to match OP_ALT after register
628 // allocation.
629 static bool
630 likely_operand_match_p (const operand_alternative &op_alt, rtx op)
631 {
632 // Empty constraints match everything.
633 const char *constraint = op_alt.constraint;
634 if (constraint[0] == 0 || constraint[0] == ',')
635 return true;
636
637 for (;;)
638 {
639 char c = *constraint;
640 int len = CONSTRAINT_LEN (c, constraint);
641 if (c == 0 || c == ',')
642 break;
643
644 if (c == 'X')
645 return true;
646
647 auto cn = lookup_constraint (constraint);
648 switch (get_constraint_type (cn))
649 {
650 case CT_REGISTER:
651 if (REG_P (op) || SUBREG_P (op))
652 return true;
653 break;
654
655 case CT_MEMORY:
656 case CT_SPECIAL_MEMORY:
657 case CT_RELAXED_MEMORY:
658 if (MEM_P (op))
659 return true;
660 break;
661
662 case CT_CONST_INT:
663 case CT_ADDRESS:
664 case CT_FIXED_FORM:
665 if (constraint_satisfied_p (op, cn))
666 return true;
667 break;
668 }
669
670 constraint += len;
671 }
672
673 if (op_alt.matches >= 0)
674 {
675 rtx other = recog_data.operand[op_alt.matches];
676 if ((REG_P (other) || SUBREG_P (other))
677 && (REG_P (op) || SUBREG_P (op)))
678 return true;
679 }
680 return false;
681 }
682
683 // Return true if the operands of the current instruction are likely to
684 // match OP_ALT.
685 static bool
686 likely_alternative_match_p (const operand_alternative *op_alt)
687 {
688 for (int i = 0; i < recog_data.n_operands; ++i)
689 if (!likely_operand_match_p (op_alt[i], recog_data.operand[i]))
690 return false;
691 return true;
692 }
693
694 // Return the sum of how disparaged OP_ALT is.
695 static int
696 count_rejects (const operand_alternative *op_alt)
697 {
698 int reject = 0;
699 for (int opno = 0; opno < recog_data.n_operands; ++opno)
700 reject += op_alt[opno].reject;
701 return reject;
702 }
703
704 // Allocate a T from the region obstack.
705 template<typename T, typename... Ts>
706 inline T *
707 early_ra::region_allocate (Ts... args)
708 {
709 static_assert (std::is_trivially_destructible<T>::value,
710 "destructor won't be called");
711 void *addr = obstack_alloc (&m_region_obstack, sizeof (T));
712 return new (addr) T (std::forward<Ts> (args)...);
713 }
714
715 early_ra::early_ra (function *fn) : m_fn (fn), m_live_fprs (0)
716 {
717 gcc_obstack_init (&m_region_obstack);
718 m_region_alloc_start = obstack_alloc (&m_region_obstack, 0);
719 bitmap_tree_view (m_live_allocnos);
720 }
721
722 early_ra::~early_ra ()
723 {
724 obstack_free (&m_region_obstack, nullptr);
725 }
726
727 // Return an array that, for each allocno A in the group, contains the index
728 // of the allocno at the head of A's chain (that is, the one with the highest
729 // START_POINT). The index is INVALID_ALLOCNO if the chain is empty.
730 inline array_slice<unsigned int>
731 early_ra::allocno_group_info::chain_heads ()
732 {
733 auto *start = reinterpret_cast<unsigned int *> (this + 1);
734 return { start, size };
735 }
736
737 // Return the array of allocnos in the group.
738 inline array_slice<early_ra::allocno_info>
739 early_ra::allocno_group_info::allocnos ()
740 {
741 gcc_checking_assert (regno != INVALID_REGNUM);
742 auto *chain_end = reinterpret_cast<unsigned int *> (this + 1) + size;
743 auto *allocno_start = reinterpret_cast<allocno_info *> (chain_end);
744 return { allocno_start, size };
745 }
746
747 // Return the group's color representative.
748 inline early_ra::allocno_group_info *
749 early_ra::allocno_group_info::color_rep ()
750 {
751 gcc_checking_assert (m_color_rep->m_color_rep == m_color_rep);
752 return m_color_rep;
753 }
754
755 // Return the group that contains the allocno.
756 inline early_ra::allocno_group_info *
757 early_ra::allocno_info::group ()
758 {
759 auto *chain_end = reinterpret_cast<unsigned int *> (this - offset);
760 return reinterpret_cast<allocno_group_info *> (chain_end - group_size) - 1;
761 }
762
763 // Return true if this allocno's live range is a subrange of related_allocno's
764 // and if we have committed to making this allocno share whatever register
765 // related_allocno uses.
766 inline bool
767 early_ra::allocno_info::is_shared ()
768 {
769 return related_allocno != INVALID_ALLOCNO && !is_equiv;
770 }
771
772 // Return true if this allocno is known to be equivalent to ALLOCNO.
773 inline bool
774 early_ra::allocno_info::is_equiv_to (unsigned int allocno)
775 {
776 return is_equiv && related_allocno == allocno;
777 }
778
779 // Return the allocnos in the subgroup.
780 inline array_slice<early_ra::allocno_info>
781 early_ra::allocno_subgroup::allocnos ()
782 {
783 if (!count)
784 return {};
785 return { &group->allocnos ()[start], count };
786 }
787
788 // Return allocno I in the subgroup, with 0 being the first.
789 inline early_ra::allocno_info *
790 early_ra::allocno_subgroup::allocno (unsigned int i)
791 {
792 return &group->allocnos ()[start + i];
793 }
794
795 // Return the previous (earlier) allocno in ALLOCNO's chain, or null if none.
796 inline early_ra::allocno_info *
797 early_ra::chain_prev (allocno_info *allocno)
798 {
799 if (allocno->chain_prev != INVALID_ALLOCNO)
800 return m_allocnos[allocno->chain_prev];
801 return nullptr;
802 }
803
804 // Return the next (later) allocno in ALLOCNO's chain, or null if none.
805 inline early_ra::allocno_info *
806 early_ra::chain_next (allocno_info *allocno)
807 {
808 if (allocno->chain_next != INVALID_ALLOCNO)
809 return m_allocnos[allocno->chain_next];
810 return nullptr;
811 }
812
813 // Dump the information in m_pseudo_regs.
814 void
815 early_ra::dump_pseudo_regs ()
816 {
817 fprintf (dump_file, "\nPseudos:\n");
818 fprintf (dump_file, " %6s %6s %6s %6s %6s %6s %8s %s\n",
819 "Id", "FPR8", "FPR16", "FPR32", "NONFPR", "Stride",
820 "FPRness", "Copies");
821 pseudo_reg_info unused_reg = {};
822 for (unsigned int regno = FIRST_PSEUDO_REGISTER;
823 regno < m_pseudo_regs.length (); ++regno)
824 {
825 const auto &reg = m_pseudo_regs[regno];
826 if (memcmp (&reg, &unused_reg, sizeof (reg)) == 0)
827 continue;
828
829 fprintf (dump_file, " %6d %6s %6s %6s %6s %6s %8d", regno,
830 reg.flags & NEEDS_FPR8 ? "Req"
831 : reg.flags & ALLOWS_FPR8 ? "OK" : "-",
832 reg.flags & NEEDS_FPR16 ? "Req"
833 : reg.flags & ALLOWS_FPR16 ? "OK" : "-",
834 reg.flags & NEEDS_FPR32 ? "Req"
835 : reg.flags & ALLOWS_FPR32 ? "OK" : "-",
836 reg.flags & NEEDS_NONFPR ? "Req"
837 : reg.flags & ALLOWS_NONFPR ? "OK" : "-",
838 ~reg.flags & HAS_FLEXIBLE_STRIDE ? "-"
839 : reg.flags & HAS_FIXED_STRIDE ? "Some" : "All",
840 fpr_preference (regno));
841 if (reg.flags & HAS_FPR_COPY)
842 fprintf (dump_file, " FPR");
843 if (reg.flags & HAS_NONFPR_COPY)
844 fprintf (dump_file, " Non-FPR");
845 unsigned int copyi = reg.first_copy;
846 while (copyi)
847 {
848 const auto &copy = m_pseudo_reg_copies[copyi];
849 if (copy.regnos[0] == regno)
850 {
851 fprintf (dump_file, " r%d", copy.regnos[1]);
852 copyi = copy.next_copies[0];
853 }
854 else
855 {
856 fprintf (dump_file, " r%d", copy.regnos[0]);
857 copyi = copy.next_copies[1];
858 }
859 }
860 fprintf (dump_file, "\n");
861 }
862 }
863
864 // Dump the information in m_fpr_ranges.
865 void
866 early_ra::dump_fpr_ranges ()
867 {
868 fprintf (dump_file, "\nFPR live ranges:\n");
869 for (unsigned int fpr = 0; fpr < 32; ++fpr)
870 {
871 auto &intervals = m_fpr_ranges[fpr];
872 if (intervals.is_empty ())
873 continue;
874
875 fprintf (dump_file, " %2d", fpr);
876 for (unsigned int i = 0; i < intervals.length (); ++i)
877 {
878 auto &interval = intervals[i];
879 if (i && (i % 4) == 0)
880 fprintf (dump_file, "\n ");
881 fprintf (dump_file, " [ %6d %6d ]", interval.start_point,
882 interval.end_point);
883 }
884 fprintf (dump_file, "\n");
885 }
886 }
887
888 // Dump the information in m_allocno_copies.
889 void
890 early_ra::dump_copies ()
891 {
892 fprintf (dump_file, "\nCopies:\n");
893 fprintf (dump_file, " %8s %3s %6s\n",
894 "Allocno", "FPR", "Weight");
895 for (const auto &copy : m_allocno_copies)
896 fprintf (dump_file, " %8d %3d %6d\n", copy.allocno,
897 copy.fpr, copy.weight);
898 }
899
900 // Dump the information in m_allocnos.
901 void
902 early_ra::dump_allocnos ()
903 {
904 char buffer[sizeof ("r[:]") + 3 * 3 * sizeof (int) + 1];
905 fprintf (dump_file, "\nAllocno groups:\n");
906 fprintf (dump_file,
907 " %12s %12s %4s %6s %8s %s\n",
908 "Ids", "Regno", "Size", "Stride", "Cands", "Heads");
909 for (unsigned int ai = 0; ai < m_allocnos.length (); ++ai)
910 {
911 auto *allocno = m_allocnos[ai];
912 if (allocno->offset != 0)
913 continue;
914 auto *group = allocno->group ();
915 snprintf (buffer, sizeof (buffer), "[%d:%d]", allocno->id,
916 allocno->id + group->size - 1);
917 fprintf (dump_file, " %12s", buffer);
918 snprintf (buffer, sizeof (buffer), "r%d[0:%d]", group->regno,
919 group->size - 1);
920 fprintf (dump_file, " %12s %4s %6d %08x", buffer,
921 group->fpr_size == FPR_D ? "D"
922 : group->fpr_size == FPR_Q ? "Q" : "Z",
923 group->stride,
924 group->fpr_candidates);
925 for (auto head : group->chain_heads ())
926 if (head == INVALID_ALLOCNO)
927 fprintf (dump_file, " -");
928 else
929 fprintf (dump_file, " %d", head);
930 fprintf (dump_file, "\n");
931 }
932
933 fprintf (dump_file, "\nAllocno chains:\n");
934 fprintf (dump_file, " %5s %12s %12s %6s %5s %5s %6s %5s\n",
935 "Id", "Regno", "Range ", "Src", "Dest", "Equiv", "Shared", "FPR");
936 for (unsigned int ai = 0; ai < m_allocnos.length (); ++ai)
937 {
938 auto *allocno = m_allocnos[ai];
939 if (allocno->chain_prev != INVALID_ALLOCNO)
940 continue;
941 const char *prefix = "=>";
942 for (;;)
943 {
944 auto *group = allocno->group ();
945 fprintf (dump_file, " %2s", prefix);
946 fprintf (dump_file, " %5d", allocno->id);
947 snprintf (buffer, sizeof (buffer), "r%d[%d]", group->regno,
948 allocno->offset);
949 fprintf (dump_file, " %12s", buffer);
950 snprintf (buffer, sizeof (buffer), "[%d,%d]",
951 allocno->start_point, allocno->end_point);
952 fprintf (dump_file, " %11s%s %6s", buffer,
953 allocno->is_earlyclobbered ? "*" : " ",
954 allocno->is_strong_copy_dest ? "Strong"
955 : allocno->is_copy_dest ? "Yes" : "-");
956 if (allocno->copy_dest == INVALID_ALLOCNO)
957 fprintf (dump_file, " %5s", "-");
958 else
959 fprintf (dump_file, " %5d", allocno->copy_dest);
960 if (allocno->is_equiv)
961 fprintf (dump_file, " %5d", allocno->related_allocno);
962 else
963 fprintf (dump_file, " %5s", "-");
964 if (allocno->is_shared ())
965 fprintf (dump_file, " %6d", allocno->related_allocno);
966 else
967 fprintf (dump_file, " %6s", "-");
968 if (allocno->hard_regno == FIRST_PSEUDO_REGISTER)
969 fprintf (dump_file, " %5s", "-");
970 else
971 fprintf (dump_file, " %5s", reg_names[allocno->hard_regno]);
972 fprintf (dump_file, "\n");
973 if (allocno->chain_next == INVALID_ALLOCNO)
974 break;
975 allocno = m_allocnos[allocno->chain_next];
976 prefix = "";
977 }
978 }
979 }
980
981 // Dump the information in m_colors.
982 void
983 early_ra::dump_colors ()
984 {
985 fprintf (dump_file, "\nColors:\n");
986 for (unsigned int i = 0; i < m_colors.length (); ++i)
987 {
988 auto *color = m_colors[i];
989 if (!color->group)
990 continue;
991
992 fprintf (dump_file, " color %d:\n", i);
993 fprintf (dump_file, " chains:\n");
994 auto heads = color->group->chain_heads ();
995 for (unsigned int i = 0; i < color->group->size; ++i)
996 {
997 fprintf (dump_file, " %2d:", i);
998 auto ai = heads[i];
999 while (ai != INVALID_ALLOCNO)
1000 {
1001 auto *allocno = m_allocnos[ai];
1002 fprintf (dump_file, " r%d[%d]", allocno->group ()->regno,
1003 allocno->offset);
1004 ai = allocno->chain_next;
1005 }
1006 fprintf (dump_file, "\n");
1007 }
1008 fprintf (dump_file, " FPR candidates:");
1009 for (unsigned int fpr = 0; fpr < 32; ++fpr)
1010 fprintf (dump_file, "%s%c", fpr % 8 ? "" : " ",
1011 color->group->fpr_candidates & (1U << fpr) ? 'Y' : '-');
1012 fprintf (dump_file, "\n");
1013 fprintf (dump_file, " FPR preferences:");
1014 for (unsigned int fpr = 0; fpr < 32; ++fpr)
1015 if (color->fpr_preferences[fpr])
1016 fprintf (dump_file, " %d(%d)", fpr, color->fpr_preferences[fpr]);
1017 fprintf (dump_file, "\n");
1018 }
1019 }
1020
1021 // Record any necessary information about a move from SRC to DEST.
1022 void
1023 early_ra::preprocess_move (rtx dest, rtx src)
1024 {
1025 if (SUBREG_P (dest))
1026 dest = SUBREG_REG (dest);
1027 if (!REG_P (dest))
1028 return;
1029
1030 if (SUBREG_P (src))
1031 src = SUBREG_REG (src);
1032 if (!REG_P (src))
1033 return;
1034
1035 // Sort the registers by increasing REGNO.
1036 rtx regs[] = { dest, src };
1037 if (REGNO (dest) > REGNO (src))
1038 std::swap (regs[0], regs[1]);
1039 unsigned int regno0 = REGNO (regs[0]);
1040 unsigned int regno1 = REGNO (regs[1]);
1041
1042 // Ignore moves between hard registers.
1043 if (HARD_REGISTER_NUM_P (regno1))
1044 return;
1045
1046 // For moves between hard registers and pseudos, just record the type
1047 // of hard register involved.
1048 auto &reg1 = m_pseudo_regs[regno1];
1049 reg1.mode = GET_MODE (regs[1]);
1050 if (HARD_REGISTER_NUM_P (regno0))
1051 {
1052 reg1.flags |= (FP_REGNUM_P (regno0) ? HAS_FPR_COPY : HAS_NONFPR_COPY);
1053 return;
1054 }
1055
1056 // Record a move between two pseudo registers.
1057 auto &reg0 = m_pseudo_regs[regno0];
1058 reg0.mode = GET_MODE (regs[0]);
1059
1060 reg_copy_info copy;
1061 copy.regnos[0] = regno0;
1062 copy.regnos[1] = regno1;
1063 copy.next_copies[0] = reg0.first_copy;
1064 copy.next_copies[1] = reg1.first_copy;
1065
1066 reg0.first_copy = reg1.first_copy = m_pseudo_reg_copies.length ();
1067 m_pseudo_reg_copies.safe_push (copy);
1068 }
1069
1070 // Return true if INSN has a multi-vector operand and if that operand
1071 // could be converted to strided form.
1072 static bool
1073 is_stride_candidate (rtx_insn *insn)
1074 {
1075 if (recog_memoized (insn) < 0)
1076 return false;
1077
1078 auto stride_type = get_attr_stride_type (insn);
1079 return (TARGET_STREAMING_SME2
1080 && (stride_type == STRIDE_TYPE_LD1_CONSECUTIVE
1081 || stride_type == STRIDE_TYPE_ST1_CONSECUTIVE));
1082 }
1083
1084 // Go through the constraints of INSN, which has already been extracted,
1085 // and record any relevant information about pseudo registers.
1086 void
1087 early_ra::process_pseudo_reg_constraints (rtx_insn *insn)
1088 {
1089 extract_insn (insn);
1090 preprocess_constraints (insn);
1091
1092 // Flags that describe any multi-register vector operands.
1093 unsigned int insn_flags = (is_stride_candidate (insn)
1094 ? HAS_FLEXIBLE_STRIDE
1095 : HAS_FIXED_STRIDE);
1096
1097 auto alts = get_preferred_alternatives (insn);
1098
1099 int operand_matches[MAX_RECOG_OPERANDS];
1100 unsigned int operand_flags[MAX_RECOG_OPERANDS];
1101 for (int i = 0; i < recog_data.n_operands; ++i)
1102 {
1103 operand_matches[i] = -1;
1104 operand_flags[i] = 0;
1105 }
1106
1107 // Extract information from the constraints, considering all plausible
1108 // alternatives.
1109 for (int altno = 0; altno < recog_data.n_alternatives; ++altno)
1110 {
1111 if (!(alts & ALTERNATIVE_BIT (altno)))
1112 continue;
1113
1114 auto *op_alt = &recog_op_alt[altno * recog_data.n_operands];
1115 if (!likely_alternative_match_p (op_alt))
1116 continue;
1117
1118 // Use SRC_OPNO's constraints to derive information about DEST_OPNO.
1119 auto record_operand = [&](int src_opno, int dest_opno)
1120 {
1121 int matches = op_alt[src_opno].matches;
1122 if (matches >= 0)
1123 operand_matches[dest_opno] = matches;
1124
1125 auto cl = alternative_class (op_alt, src_opno);
1126 if (cl != NO_REGS)
1127 {
1128 if (reg_class_subset_p (cl, FP_REGS))
1129 operand_flags[dest_opno] |= ALLOWS_FPR32;
1130 if (reg_class_subset_p (cl, FP_LO_REGS))
1131 operand_flags[dest_opno] |= ALLOWS_FPR16;
1132 if (reg_class_subset_p (cl, FP_LO8_REGS))
1133 operand_flags[dest_opno] |= ALLOWS_FPR8;
1134 if (!reg_classes_intersect_p (cl, FP_REGS))
1135 operand_flags[dest_opno] |= ALLOWS_NONFPR;
1136 }
1137 };
1138
1139 for (int i = 0; i < recog_data.n_operands; ++i)
1140 {
1141 record_operand (i, i);
1142 if (recog_data.constraints[i][0] == '%')
1143 {
1144 record_operand (i, i + 1);
1145 record_operand (i + 1, i);
1146 }
1147 }
1148 }
1149
1150 // Process the information we collected above.
1151 for (int i = 0; i < recog_data.n_operands; ++i)
1152 {
1153 rtx op = recog_data.operand[i];
1154 machine_mode orig_mode = GET_MODE (op);
1155 if (SUBREG_P (op))
1156 op = SUBREG_REG (op);
1157
1158 // Record the accumulated information in m_pseudo_regs.
1159 if (REG_P (op) && !HARD_REGISTER_P (op))
1160 {
1161 // The flags so far just describe what at least one alternative
1162 // would accept. Calculate the associated NEEDS_* information.
1163 auto flags = operand_flags[i];
1164 if (!(flags & ALLOWS_FPR32) && (flags & ALLOWS_NONFPR))
1165 flags |= NEEDS_NONFPR;
1166 else if ((flags & ALLOWS_FPR32) && !(flags & ALLOWS_NONFPR))
1167 {
1168 if (flags & ALLOWS_FPR8)
1169 flags |= NEEDS_FPR8;
1170 if (flags & ALLOWS_FPR16)
1171 flags |= NEEDS_FPR16;
1172 flags |= NEEDS_FPR32;
1173 }
1174
1175 // Look for multi-register vector operands.
1176 if (VECTOR_MODE_P (orig_mode)
1177 && targetm.hard_regno_mode_ok (V0_REGNUM, orig_mode)
1178 && hard_regno_nregs (V0_REGNUM, orig_mode) > 1)
1179 flags |= insn_flags;
1180
1181 m_pseudo_regs[REGNO (op)].flags |= flags;
1182 m_pseudo_regs[REGNO (op)].mode = GET_MODE (op);
1183 }
1184
1185 // Treat matching constraints as equivalent to moves.
1186 if (operand_matches[i] >= 0)
1187 preprocess_move (recog_data.operand[operand_matches[i]], op);
1188 }
1189 }
1190
1191 // Make one pass through the instructions, collecting information that
1192 // will be needed later.
1193 void
1194 early_ra::preprocess_insns ()
1195 {
1196 m_pseudo_regs.safe_grow_cleared (max_reg_num ());
1197 m_pseudo_reg_copies.safe_push (reg_copy_info ());
1198 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
1199 {
1200 if (!NONDEBUG_INSN_P (insn))
1201 continue;
1202
1203 // Mark all registers that occur in addresses as needing a GPR.
1204 vec_rtx_properties properties;
1205 properties.add_insn (insn, true);
1206 for (rtx_obj_reference ref : properties.refs ())
1207 if (ref.is_reg ()
1208 && ref.in_address ()
1209 && !HARD_REGISTER_NUM_P (ref.regno))
1210 m_pseudo_regs[ref.regno].flags |= ALLOWS_NONFPR | NEEDS_NONFPR;
1211
1212 if (GET_CODE (PATTERN (insn)) == USE
1213 || GET_CODE (PATTERN (insn)) == CLOBBER)
1214 continue;
1215
1216 rtx set = single_set (insn);
1217 if (set && is_move_set (set))
1218 preprocess_move (SET_DEST (set), SET_SRC (set));
1219 else
1220 process_pseudo_reg_constraints (insn);
1221 }
1222 }
1223
1224 // Return a signed integer that says (roughly) how strong an affinity
1225 // pseudo register REGNO has with FPRs. A positive value indicates
1226 // that we should try to allocate an FPR, a negative value indicates
1227 // that we shouldn't, and 0 indicates neutrality.
1228 int
1229 early_ra::fpr_preference (unsigned int regno)
1230 {
1231 auto mode = m_pseudo_regs[regno].mode;
1232 auto flags = m_pseudo_regs[regno].flags;
1233 if (flags & IGNORE_REG)
1234 return -4;
1235 else if (mode == VOIDmode || !targetm.hard_regno_mode_ok (V0_REGNUM, mode))
1236 return -3;
1237 else if (flags & HAS_FLEXIBLE_STRIDE)
1238 return 3;
1239 else if (flags & NEEDS_FPR32)
1240 return 2;
1241 else if (!(flags & ALLOWS_FPR32) && (flags & ALLOWS_NONFPR))
1242 return -2;
1243 else if ((flags & HAS_FPR_COPY) && !(flags & HAS_NONFPR_COPY))
1244 return 1;
1245 else if ((flags & HAS_NONFPR_COPY) && !(flags & HAS_FPR_COPY))
1246 return -1;
1247 else
1248 return 0;
1249 }
1250
1251 // Propagate information about pseudo-registers along copy edges,
1252 // while doing so doesn't create conflicting FPR preferences.
1253 void
1254 early_ra::propagate_pseudo_reg_info ()
1255 {
1256 struct stack_entry { unsigned int regno, copyi; };
1257
1258 auto_vec<stack_entry, 32> stack;
1259 for (unsigned int i = FIRST_PSEUDO_REGISTER;
1260 i < m_pseudo_regs.length (); ++i)
1261 {
1262 auto start = m_pseudo_regs[i].first_copy;
1263 if (!start)
1264 continue;
1265
1266 stack.quick_push ({ i, start });
1267 while (!stack.is_empty ())
1268 {
1269 auto entry = stack.pop ();
1270 auto &copy = m_pseudo_reg_copies[entry.copyi];
1271 auto src_regno = entry.regno;
1272 auto dest_regno = (src_regno == copy.regnos[1]
1273 ? copy.regnos[0]
1274 : copy.regnos[1]);
1275 auto next_copyi = (src_regno == copy.regnos[1]
1276 ? copy.next_copies[1]
1277 : copy.next_copies[0]);
1278 if (next_copyi)
1279 stack.safe_push ({ src_regno, next_copyi });
1280
1281 auto &src_reg = m_pseudo_regs[src_regno];
1282 auto &dest_reg = m_pseudo_regs[dest_regno];
1283
1284 if (src_reg.flags & ~dest_reg.flags & PSEUDO_COPY_FLAGS)
1285 {
1286 auto src_preference = fpr_preference (src_regno);
1287 auto dest_preference = fpr_preference (dest_regno);
1288 if ((src_preference >= 0 && dest_preference >= 0)
1289 || (src_preference <= 0 && dest_preference <= 0))
1290 {
1291 dest_reg.flags |= (src_reg.flags & PSEUDO_COPY_FLAGS);
1292 stack.safe_push ({ dest_regno, dest_reg.first_copy });
1293 }
1294 }
1295 }
1296 }
1297 }
1298
1299 // Decide which pseudos should be allocated an FPR, setting m_fpr_pseudos
1300 // accordingly.
1301 void
1302 early_ra::choose_fpr_pseudos ()
1303 {
1304 for (unsigned int i = FIRST_PSEUDO_REGISTER;
1305 i < m_pseudo_regs.length (); ++i)
1306 if (fpr_preference (i) > 0)
1307 bitmap_set_bit (m_fpr_pseudos, i);
1308 }
1309
1310 // Clear out information about the previous CFG region (if any)
1311 // and set up the data for a new region.
1312 void
1313 early_ra::start_new_region ()
1314 {
1315 obstack_free (&m_region_obstack, m_region_alloc_start);
1316 m_regno_to_group.empty ();
1317 m_allocno_copies.truncate (0);
1318 m_allocnos.truncate (0);
1319 m_sorted_allocnos.truncate (0);
1320 m_shared_allocnos.truncate (0);
1321 m_colors.truncate (0);
1322 m_insn_ranges.truncate (0);
1323 for (auto &fpr_ranges : m_fpr_ranges)
1324 fpr_ranges.truncate (0);
1325 for (auto &call_points : m_call_points)
1326 call_points.truncate (0);
1327 gcc_assert (bitmap_empty_p (m_live_allocnos) && m_live_fprs == 0);
1328 m_dead_insns.truncate (0);
1329 m_allocated_fprs = 0;
1330 m_call_preserved_fprs = 0;
1331 m_accurate_live_ranges = true;
1332 m_allocation_successful = true;
1333 m_current_region += 1;
1334 }
1335
1336 // Record that we can no longer track the liveness of individual allocnos.
1337 // Call DUMP to dump the reason to a dump file.
1338 template<typename T>
1339 void
1340 early_ra::record_live_range_failure (T dump)
1341 {
1342 if (!m_accurate_live_ranges)
1343 return;
1344
1345 m_accurate_live_ranges = false;
1346 m_allocation_successful = false;
1347 if (dump_file && (dump_flags & TDF_DETAILS))
1348 {
1349 fprintf (dump_file, "Unable to track live ranges further: ");
1350 dump ();
1351 fprintf (dump_file, "\n");
1352 }
1353 }
1354
1355 // Record that the allocation of the current region has filed. Call DUMP to
1356 // dump the reason to a dump file.
1357 template<typename T>
1358 void
1359 early_ra::record_allocation_failure (T dump)
1360 {
1361 if (!m_allocation_successful)
1362 return;
1363
1364 m_allocation_successful = false;
1365 if (dump_file && (dump_flags & TDF_DETAILS))
1366 {
1367 fprintf (dump_file, "Not allocating region: ");
1368 dump ();
1369 fprintf (dump_file, "\n");
1370 }
1371 }
1372
1373 // Create and return an allocno group of size SIZE for register REGNO.
1374 // REGNO can be INVALID_REGNUM if the group just exists to allow
1375 // other groups to be chained together, and does not have any new
1376 // allocnos of its own.
1377 early_ra::allocno_group_info *
1378 early_ra::create_allocno_group (unsigned int regno, unsigned int size)
1379 {
1380 static_assert (alignof (unsigned int) == alignof (allocno_info),
1381 "allocno_info alignment");
1382 unsigned int num_allocnos = (regno != INVALID_REGNUM ? size : 0);
1383
1384 // Allocate an allocno_group_info, followed by an array of chain heads,
1385 // followed by the allocnos themselves.
1386 size_t alloc_size = (sizeof (allocno_group_info)
1387 + size * sizeof (unsigned int)
1388 + num_allocnos * sizeof (allocno_info));
1389 void *data = obstack_alloc (&m_region_obstack, alloc_size);
1390
1391 // Initialize the group.
1392 auto *group = reinterpret_cast<allocno_group_info *> (data);
1393 memset (group, 0, sizeof (*group));
1394 group->m_color_rep = group;
1395 group->regno = regno;
1396 group->size = size;
1397 group->stride = 1;
1398 group->fpr_size = FPR_D;
1399 group->fpr_candidates = ~0U;
1400
1401 // Initialize the chain heads.
1402 auto heads = group->chain_heads ();
1403 for (unsigned int i = 0; i < heads.size (); ++i)
1404 heads[i] = (i < num_allocnos ? m_allocnos.length () + i : INVALID_ALLOCNO);
1405
1406 // Initialize the allocnos.
1407 if (num_allocnos > 0)
1408 {
1409 auto allocnos = group->allocnos ();
1410 memset (allocnos.begin (), 0, num_allocnos * sizeof (allocno_info));
1411 for (unsigned int i = 0; i < num_allocnos; ++i)
1412 {
1413 auto *allocno = &allocnos[i];
1414 allocno->id = m_allocnos.length ();
1415 allocno->offset = i;
1416 allocno->group_size = size;
1417 allocno->hard_regno = FIRST_PSEUDO_REGISTER;
1418 allocno->start_point = END_OF_REGION;
1419 allocno->end_point = START_OF_REGION;
1420 allocno->copy_dest = INVALID_ALLOCNO;
1421 allocno->related_allocno = INVALID_ALLOCNO;
1422 allocno->chain_next = INVALID_ALLOCNO;
1423 allocno->chain_prev = INVALID_ALLOCNO;
1424 m_allocnos.safe_push (allocno);
1425 }
1426 }
1427 return group;
1428 }
1429
1430 // If REG refers to a pseudo register that might be allocated to FPRs,
1431 // return the associated range of allocnos, creating new ones if necessary.
1432 // Return an empty range otherwise.
1433 early_ra::allocno_subgroup
1434 early_ra::get_allocno_subgroup (rtx reg)
1435 {
1436 if (GET_CODE (reg) == SUBREG)
1437 {
1438 allocno_subgroup inner = get_allocno_subgroup (SUBREG_REG (reg));
1439 if (!inner)
1440 return {};
1441
1442 if (!targetm.can_change_mode_class (GET_MODE (SUBREG_REG (reg)),
1443 GET_MODE (reg), FP_REGS))
1444 {
1445 record_live_range_failure ([&](){
1446 fprintf (dump_file, "cannot refer to r%d:%s in mode %s",
1447 REGNO (SUBREG_REG (reg)),
1448 GET_MODE_NAME (GET_MODE (SUBREG_REG (reg))),
1449 GET_MODE_NAME (GET_MODE (reg)));
1450 });
1451 return {};
1452 }
1453
1454 if (!targetm.modes_tieable_p (GET_MODE (SUBREG_REG (reg)),
1455 GET_MODE (reg)))
1456 record_allocation_failure ([&](){
1457 fprintf (dump_file, "r%d's mode %s is not tieable to mode %s",
1458 REGNO (SUBREG_REG (reg)),
1459 GET_MODE_NAME (GET_MODE (SUBREG_REG (reg))),
1460 GET_MODE_NAME (GET_MODE (reg)));
1461 });
1462
1463 subreg_info info;
1464 subreg_get_info (V0_REGNUM, GET_MODE (SUBREG_REG (reg)),
1465 SUBREG_BYTE (reg), GET_MODE (reg), &info);
1466 if (!info.representable_p)
1467 {
1468 record_live_range_failure ([&](){
1469 fprintf (dump_file, "subreg of r%d is invalid for V0",
1470 REGNO (SUBREG_REG (reg)));
1471 });
1472 return {};
1473 }
1474
1475 inner.start += info.offset;
1476 inner.count = info.nregs;
1477 return inner;
1478 }
1479
1480 if (!REG_P (reg) || HARD_REGISTER_P (reg))
1481 return {};
1482
1483 unsigned int regno = REGNO (reg);
1484 if (fpr_preference (regno) <= 0)
1485 return {};
1486
1487 unsigned int count = hard_regno_nregs (V0_REGNUM, GET_MODE (reg));
1488 bool existed;
1489 auto &entry = m_regno_to_group.get_or_insert (regno, &existed);
1490 if (!existed)
1491 {
1492 auto *group = create_allocno_group (regno, count);
1493 if (dump_file && (dump_flags & TDF_DETAILS))
1494 {
1495 auto allocnos = group->allocnos ();
1496 fprintf (dump_file, "Creating allocnos [%d:%d] for r%d\n",
1497 allocnos.front ().id, allocnos.back ().id, regno);
1498 }
1499
1500 auto reg_bits = GET_MODE_BITSIZE (GET_MODE (reg));
1501 auto fpr_bits = exact_div (reg_bits, count);
1502 auto flags = m_pseudo_regs[regno].flags;
1503
1504 // Punt for now if there is a choice to be made between using an
1505 // FPR and a non-FPR.
1506 if ((flags & NEEDS_NONFPR)
1507 || ((flags & ALLOWS_NONFPR)
1508 && !FLOAT_MODE_P (GET_MODE (reg))
1509 && !VECTOR_MODE_P (GET_MODE (reg))))
1510 record_allocation_failure ([&](){
1511 fprintf (dump_file, "r%d has FPR and non-FPR references", regno);
1512 });
1513
1514 if (flags & ALLOWS_FPR8)
1515 group->fpr_candidates &= 0xff;
1516 else if (flags & ALLOWS_FPR16)
1517 group->fpr_candidates &= 0xffff;
1518 group->fpr_candidates &= ~0U >> (count - 1);
1519
1520 group->has_flexible_stride = ((flags & HAS_FLEXIBLE_STRIDE) != 0
1521 && (flags & HAS_FIXED_STRIDE) == 0);
1522
1523 // SVE modes always occupy the full Z register, even for partial modes
1524 // like VNx2SF whose mode size is only 64 bits. Treating such modes
1525 // as FPR_D would let partial_fpr_clobbers ignore the fact that
1526 // V8-V15 are only partially callee-saved under AAPCS64 -- the high
1527 // bits of the Z register are clobbered by calls. Always classify
1528 // SVE modes as FPR_Z so that V8-V15 are excluded as candidates for
1529 // pseudos that are live across calls.
1530 group->fpr_size = ((aarch64_sve_mode_p (GET_MODE (reg))
1531 || maybe_gt (fpr_bits, 128)) ? FPR_Z
1532 : maybe_gt (fpr_bits, 64) ? FPR_Q : FPR_D);
1533
1534 entry = group;
1535 }
1536 return { entry, 0, count };
1537 }
1538
1539 // Record a use of FPR REGNO at the current program point, as part of
1540 // a backwards walk over a block.
1541 void
1542 early_ra::record_fpr_use (unsigned int regno)
1543 {
1544 gcc_assert (IN_RANGE (regno, V0_REGNUM, V31_REGNUM));
1545 unsigned int offset = regno - V0_REGNUM;
1546 if (!(m_live_fprs & (1U << offset)))
1547 {
1548 m_fpr_ranges[offset].safe_push ({ START_OF_REGION, m_current_point,
1549 INVALID_ALLOCNO });
1550 m_live_fprs |= 1U << offset;
1551 }
1552 }
1553
1554 // Record a definition of FPR REGNO at the current program point, as part of
1555 // a backwards walk over a block.
1556 void
1557 early_ra::record_fpr_def (unsigned int regno)
1558 {
1559 gcc_assert (IN_RANGE (regno, V0_REGNUM, V31_REGNUM));
1560 unsigned int offset = regno - V0_REGNUM;
1561
1562 // The definition completes the current live range. If the result
1563 // of the definition is used, the live range extends to the last use.
1564 // Otherwise the live range is just a momentary blip at the current point.
1565 auto &ranges = m_fpr_ranges[offset];
1566 if (m_live_fprs & (1U << offset))
1567 {
1568 ranges.last ().start_point = m_current_point;
1569 m_live_fprs &= ~(1U << offset);
1570 }
1571 else
1572 ranges.safe_push ({ m_current_point, m_current_point, INVALID_ALLOCNO });
1573 }
1574
1575 // Record a use of allocno ALLOCNO at the current program point, as part
1576 // of a backwards walk over a block.
1577 void
1578 early_ra::record_allocno_use (allocno_info *allocno)
1579 {
1580 if (allocno->start_point == m_current_point)
1581 return;
1582
1583 gcc_checking_assert (!allocno->is_shared ());
1584 bitmap_set_bit (m_live_allocnos, allocno->id);
1585 if (allocno->end_point > m_current_point)
1586 {
1587 allocno->end_point = m_current_point;
1588 allocno->last_def_point = START_OF_REGION;
1589 allocno->last_use_point = END_OF_REGION;
1590 }
1591 else
1592 allocno->last_use_point = allocno->start_point;
1593 allocno->start_point = m_current_point;
1594 allocno->is_copy_dest = false;
1595 allocno->is_strong_copy_src = false;
1596 allocno->related_allocno = INVALID_ALLOCNO;
1597 allocno->is_equiv = false;
1598 }
1599
1600 // Record a definition of the allocno with index AI at the current program
1601 // point, as part of a backwards walk over a block. The allocno is known
1602 // to be live.
1603 void
1604 early_ra::record_allocno_def (allocno_info *allocno)
1605 {
1606 gcc_checking_assert (!allocno->is_shared ());
1607 allocno->last_use_point = allocno->start_point;
1608 allocno->last_def_point = m_current_point;
1609 allocno->start_point = m_current_point;
1610 allocno->num_defs = MIN (allocno->num_defs + 1, 2);
1611 gcc_checking_assert (!allocno->is_copy_dest
1612 && !allocno->is_strong_copy_src);
1613 if (!bitmap_clear_bit (m_live_allocnos, allocno->id))
1614 gcc_unreachable ();
1615 }
1616
1617 // SRC_ALLOCNO is copied or tied to DEST_ALLOCNO; IS_EQUIV is true if the
1618 // two allocnos are known to be equal. See whether we can mark a chain of
1619 // allocnos ending at DEST_ALLOCNO as related to SRC_ALLOCNO. Return the
1620 // start of the chain if so, otherwise return null.
1621 //
1622 // If IS_EQUIV, a chain that contains just DEST_ALLOCNO should be treated
1623 // as an equivalence. Otherwise the chain should be shared with SRC_ALLOCNO.
1624 //
1625 // Sharing chains are a rather hacky workaround for the fact that we
1626 // don't collect segmented live ranges, and that in the end we want to do
1627 // simple interval graph coloring.
1628 early_ra::allocno_info *
1629 early_ra::find_related_start (allocno_info *dest_allocno,
1630 allocno_info *src_allocno, bool is_equiv)
1631 {
1632 allocno_info *res = nullptr;
1633 for (;;)
1634 {
1635 if (src_allocno->end_point > dest_allocno->end_point)
1636 // The src allocno dies first.
1637 return res;
1638
1639 if (src_allocno->num_defs != 0)
1640 {
1641 if (dest_allocno->end_point < m_current_bb_point)
1642 // We don't currently track enough information to handle multiple
1643 // definitions across basic block boundaries.
1644 return res;
1645
1646 if (src_allocno->last_def_point >= dest_allocno->end_point)
1647 // There is another definition during the destination's live range.
1648 return res;
1649 }
1650 if (is_equiv)
1651 {
1652 if (dest_allocno->num_defs == 1)
1653 // dest_allocno is equivalent to src_allocno for dest_allocno's
1654 // entire live range. Fall back to that if we can't establish
1655 // a sharing chain.
1656 res = dest_allocno;
1657 }
1658 else
1659 {
1660 if (src_allocno->last_use_point >= dest_allocno->end_point)
1661 // src_allocno is live during dest_allocno's live range,
1662 // and the two allocnos do not necessarily have the same value.
1663 return res;
1664 }
1665
1666 if (dest_allocno->group_size != 1
1667 // Account for definitions by shared registers.
1668 || dest_allocno->num_defs > 1
1669 || DF_REG_DEF_COUNT (dest_allocno->group ()->regno) != 1)
1670 // Currently only single allocnos that are defined once can
1671 // share registers with non-equivalent allocnos. This could be
1672 // relaxed, but at the time of writing, aggregates are not valid
1673 // SSA names and so generally only use a single pseudo throughout
1674 // their lifetime.
1675 return res;
1676
1677 if (dest_allocno->copy_dest == src_allocno->id)
1678 // We've found a complete and valid sharing chain.
1679 return dest_allocno;
1680
1681 if (dest_allocno->copy_dest == INVALID_ALLOCNO)
1682 return res;
1683
1684 auto *next_allocno = m_allocnos[dest_allocno->copy_dest];
1685 if (!is_chain_candidate (dest_allocno, next_allocno, ALL_REASONS))
1686 return res;
1687
1688 dest_allocno = next_allocno;
1689 is_equiv = false;
1690 }
1691 }
1692
1693 // Add FROM_ALLOCNO's definition information to TO_ALLOCNO's.
1694 void
1695 early_ra::accumulate_defs (allocno_info *to_allocno,
1696 allocno_info *from_allocno)
1697 {
1698 if (from_allocno->num_defs > 0)
1699 {
1700 to_allocno->num_defs = MIN (from_allocno->num_defs
1701 + to_allocno->num_defs, 2);
1702 to_allocno->last_def_point = MAX (to_allocno->last_def_point,
1703 from_allocno->last_def_point);
1704 }
1705 }
1706
1707 // Record any relevant allocno-related information for an actual or imagined
1708 // copy from SRC to DEST. FROM_MOVE_P is true if the copy was an explicit
1709 // move instruction, false if it represents one way of satisfying the previous
1710 // instruction's constraints.
1711 void
1712 early_ra::record_copy (rtx dest, rtx src, bool from_move_p)
1713 {
1714 auto dest_range = get_allocno_subgroup (dest);
1715 auto src_range = get_allocno_subgroup (src);
1716 if (from_move_p
1717 && dest_range
1718 && REG_P (src)
1719 && FP_REGNUM_P (REGNO (src)))
1720 {
1721 // A copy from an FPR to an allocno group.
1722 unsigned int fpr = REGNO (src) - V0_REGNUM;
1723 m_allocno_copies.safe_push ({ dest_range.allocno (0)->id, fpr,
1724 dest_range.count });
1725
1726 // If the allocno at the other end of the chain of copies from DEST
1727 // has a copy to the same FPR, record that all intervening copy chains
1728 // could become "strong" ones. This indicates that picking the FPR
1729 // avoids a copy at both ends.
1730 unsigned int hard_regno = REGNO (src);
1731 for (auto &dest_allocno : dest_range.allocnos ())
1732 if (dest_allocno.hard_regno == hard_regno++)
1733 dest_allocno.is_strong_copy_src = true;
1734 }
1735 else if (from_move_p
1736 && src_range
1737 && REG_P (dest)
1738 && FP_REGNUM_P (REGNO (dest)))
1739 {
1740 // A copy from an allocno group to an FPR.
1741 unsigned int fpr = REGNO (dest) - V0_REGNUM;
1742 m_allocno_copies.safe_push ({ src_range.allocno (0)->id, fpr,
1743 src_range.count });
1744 for (auto &src_allocno : src_range.allocnos ())
1745 {
1746 // If the copy comes from a move, see whether the destination
1747 // FPR is known to be equal to the source allocno for the FPR's
1748 // last live range.
1749 if (from_move_p && src_allocno.num_defs == 0)
1750 {
1751 auto &last_range = m_fpr_ranges[fpr].last ();
1752 if (last_range.end_point >= src_allocno.end_point)
1753 last_range.allocno = src_allocno.id;
1754 }
1755 src_allocno.hard_regno = V0_REGNUM + fpr;
1756 fpr += 1;
1757 }
1758 }
1759 else if (src_range && dest_range)
1760 {
1761 // A copy between two allocno groups. We can only have a mismatched
1762 // number of FPRs for imaginary, non-move copies. In that case
1763 // the matching happens on the common lowparts.
1764 gcc_assert (!from_move_p || src_range.count == dest_range.count);
1765 unsigned int count = std::min (src_range.count, dest_range.count);
1766 if (WORDS_BIG_ENDIAN)
1767 {
1768 src_range.start += src_range.count - count;
1769 dest_range.start += dest_range.count - count;
1770 }
1771 src_range.count = count;
1772 dest_range.count = count;
1773
1774 // Ignore (imaginary non-move) copies if the destination is still live.
1775 for (auto &dest_allocno : dest_range.allocnos ())
1776 if (bitmap_bit_p (m_live_allocnos, dest_allocno.id))
1777 return;
1778
1779 for (unsigned int i = 0; i < src_range.count; ++i)
1780 {
1781 auto *dest_allocno = dest_range.allocno (i);
1782 auto *src_allocno = src_range.allocno (i);
1783 if (src_allocno->end_point > dest_allocno->start_point)
1784 {
1785 gcc_assert (src_allocno->copy_dest == INVALID_ALLOCNO
1786 || src_allocno->copy_dest == dest_allocno->id);
1787 src_allocno->copy_dest = dest_allocno->id;
1788 src_allocno->hard_regno = dest_allocno->hard_regno;
1789 dest_allocno->is_copy_dest = 1;
1790 }
1791 else if (auto *start_allocno = find_related_start (dest_allocno,
1792 src_allocno,
1793 from_move_p))
1794 {
1795 auto *next_allocno = dest_allocno;
1796 for (;;)
1797 {
1798 next_allocno->related_allocno = src_allocno->id;
1799 next_allocno->is_equiv = (start_allocno == dest_allocno
1800 && from_move_p);
1801 // If we're sharing two allocnos that are not equivalent,
1802 // carry across the definition information. This is needed
1803 // to prevent multiple incompatible attempts to share with
1804 // the same register.
1805 if (next_allocno->is_shared ())
1806 accumulate_defs (src_allocno, next_allocno);
1807 src_allocno->last_use_point
1808 = MAX (src_allocno->last_use_point,
1809 next_allocno->last_use_point);
1810
1811 if (next_allocno == start_allocno)
1812 break;
1813 next_allocno = m_allocnos[next_allocno->copy_dest];
1814 }
1815 }
1816 }
1817 }
1818 }
1819
1820 // Record any relevant allocno-related information about the constraints
1821 // on INSN, which has already been extracted.
1822 void
1823 early_ra::record_constraints (rtx_insn *insn)
1824 {
1825 preprocess_constraints (insn);
1826
1827 int operand_matches[MAX_RECOG_OPERANDS];
1828 for (int i = 0; i < recog_data.n_operands; ++i)
1829 operand_matches[i] = -1;
1830
1831 auto alts = get_preferred_alternatives (insn);
1832 bool any_ok = recog_data.n_alternatives == 0;
1833
1834 // The set of output operands that are earlyclobber in at least one
1835 // alternative.
1836 operand_mask earlyclobber_operands = 0;
1837
1838 // The set of output operands that are matched to inputs in at least
1839 // one alternative.
1840 operand_mask matched_operands = 0;
1841
1842 // The set of output operands that are not matched to inputs in at least
1843 // one alternative.
1844 operand_mask unmatched_operands = 0;
1845
1846 // The set of input operands that are matched to outputs in at least one
1847 // alternative, or that overlap with such an input if the output is not
1848 // earlyclobber. The latter part of the condition copes with things
1849 // like y = x * x, where the first x is tied to the destination, and where
1850 // y is not earlyclobber.
1851 operand_mask matches_operands = 0;
1852
1853 for (int altno = 0; altno < recog_data.n_alternatives; ++altno)
1854 {
1855 if (!(alts & ALTERNATIVE_BIT (altno)))
1856 continue;
1857
1858 auto *op_alt = &recog_op_alt[altno * recog_data.n_operands];
1859 if (!likely_alternative_match_p (op_alt))
1860 continue;
1861
1862 any_ok = true;
1863
1864 // Update the information for operand DEST_OPNO based on the constraint
1865 // information for operand SRC_OPNO. The numbers can be different for
1866 // swapped commutative operands.
1867 auto record_operand = [&](int src_opno, int dest_opno)
1868 {
1869 int matches = op_alt[src_opno].matches;
1870 // A matched earlyclobber cannot be used if the same operand value
1871 // occurs in an unmatched operand. E.g. for y = x * x, a matched
1872 // earlyclobber on the first input does not cover the second input.
1873 if (matches >= 0)
1874 {
1875 rtx op = recog_data.operand[dest_opno];
1876 operand_mask overlaps = 0;
1877 for (int i = 0; i < recog_data.n_operands; ++i)
1878 if (i != dest_opno
1879 && !recog_data.is_operator[i]
1880 && recog_data.operand_type[i] != OP_OUT
1881 && reg_overlap_mentioned_p (op, recog_data.operand[i]))
1882 overlaps |= 1U << i;
1883 if (!op_alt[matches].earlyclobber || overlaps == 0)
1884 {
1885 operand_matches[dest_opno] = matches;
1886 matches_operands |= (1U << dest_opno) | overlaps;
1887 }
1888 }
1889 };
1890
1891 auto reject = count_rejects (op_alt);
1892 for (int opno = 0; opno < recog_data.n_operands; ++opno)
1893 {
1894 operand_mask op_mask = operand_mask (1) << opno;
1895
1896 if (recog_data.operand_type[opno] != OP_IN)
1897 {
1898 if (reject == 0 && op_alt[opno].matched >= 0)
1899 matched_operands |= op_mask;
1900 else
1901 unmatched_operands |= op_mask;
1902 }
1903
1904 if (op_alt[opno].earlyclobber)
1905 earlyclobber_operands |= op_mask;
1906
1907 // Punt for now on scratches. If we wanted to handle them,
1908 // we'd need to create allocnos for them, like IRA does.
1909 rtx op = recog_data.operand[opno];
1910 if (GET_CODE (op) == SCRATCH
1911 && reg_classes_intersect_p (op_alt[opno].cl, FP_REGS))
1912 record_allocation_failure ([&](){
1913 fprintf (dump_file, "insn %d has FPR match_scratch",
1914 INSN_UID (insn));
1915 });
1916
1917 // Record filter information, which applies to the first register
1918 // in the operand.
1919 if (auto filters = alternative_register_filters (op_alt, opno))
1920 if (auto range = get_allocno_subgroup (recog_data.operand[opno]))
1921 for (unsigned int fpr = range.start; fpr < 32; ++fpr)
1922 if (!test_register_filters (filters, fpr))
1923 range.group->fpr_candidates &= ~(1U << (fpr - range.start));
1924
1925 if (reject == 0)
1926 {
1927 // Record possible matched operands.
1928 record_operand (opno, opno);
1929 if (recog_data.constraints[opno][0] == '%')
1930 {
1931 record_operand (opno, opno + 1);
1932 record_operand (opno + 1, opno);
1933 }
1934 }
1935 }
1936 }
1937
1938 if (!any_ok)
1939 {
1940 if (dump_file && (dump_flags & TDF_DETAILS))
1941 fprintf (dump_file, " -- no match\n");
1942 record_allocation_failure ([&](){
1943 fprintf (dump_file, "no matching constraints for insn %d",
1944 INSN_UID (insn));
1945 });
1946 }
1947
1948 rtx dest_op = NULL_RTX;
1949 for (int opno = 0; opno < recog_data.n_operands; ++opno)
1950 {
1951 rtx op = recog_data.operand[opno];
1952 auto op_mask = operand_mask (1) << opno;
1953 // Record if there is an output operand that is "independent" of the
1954 // inputs, in the sense that it is never earlyclobber and is never
1955 // matched to an input. See the comment below for how this is used.
1956 if (recog_data.operand_type[opno] == OP_OUT
1957 && (earlyclobber_operands & op_mask) == 0
1958 && (matched_operands & op_mask) == 0)
1959 {
1960 dest_op = op;
1961 break;
1962 }
1963 // We sometimes decide not to allocate pseudos even if they meet
1964 // the normal FPR preference criteria; see the setters of IGNORE_REG
1965 // for details. However, the premise is that we should only ignore
1966 // definitions of such pseudos if they are independent of the other
1967 // operands in the instruction.
1968 else if (recog_data.operand_type[opno] != OP_IN
1969 && REG_P (op)
1970 && !HARD_REGISTER_P (op)
1971 && (m_pseudo_regs[REGNO (op)].flags & IGNORE_REG) != 0)
1972 record_allocation_failure ([&](){
1973 fprintf (dump_file, "ignored register r%d depends on input operands",
1974 REGNO (op));
1975 });
1976 }
1977
1978 for (int opno = 0; opno < recog_data.n_operands; ++opno)
1979 {
1980 auto op_mask = operand_mask (1) << opno;
1981 rtx op = recog_data.operand[opno];
1982 int matches = operand_matches[opno];
1983
1984 // Punt for now on operands that already have a fixed choice of
1985 // register, since we don't have IRA's ability to find an alternative.
1986 // It's better if earlier passes don't create this kind of situation.
1987 if (REG_P (op) && FP_REGNUM_P (REGNO (op)))
1988 record_allocation_failure ([&](){
1989 fprintf (dump_file, "operand %d of insn %d refers directly to %s",
1990 opno, INSN_UID (insn), reg_names[REGNO (op)]);
1991 });
1992
1993 // Treat input operands as being earlyclobbered if an output is
1994 // sometimes earlyclobber and if the input never matches an output.
1995 // Do the same if there is an output that is always matched to an
1996 // input, and if this operand doesn't match that input. In both
1997 // cases, tying the input and the output would lead to an impossible
1998 // combination (or at least one that is difficult to reload).
1999 if (recog_data.operand_type[opno] != OP_OUT
2000 && ((earlyclobber_operands && matches < 0)
2001 || ((matched_operands & ~unmatched_operands)
2002 && !(matches_operands & op_mask))))
2003 for (auto &allocno : get_allocno_subgroup (op).allocnos ())
2004 if (allocno.end_point + 1 == m_current_point)
2005 allocno.is_earlyclobbered = true;
2006
2007 // Create copies between operands that can be tied. This (deliberately)
2008 // might add several copies to the same destination register; later code
2009 // can then choose between them based on other criteria.
2010 //
2011 // If there is an output operand that is never matched or earlyclobber,
2012 // and an input operand that never matches an output operand, create
2013 // a tentative copy between them. This allows hard register preferences
2014 // to be transmitted along the copy chains.
2015 if (matches >= 0)
2016 record_copy (recog_data.operand[matches], op);
2017 else if (dest_op && recog_data.operand_type[opno] == OP_IN)
2018 record_copy (dest_op, op);
2019 }
2020 }
2021
2022 // If FLAGS is DF_REF_AT_TOP, model the artificial uses and defs at the
2023 // start of the current basic block, otherwise model the artificial uses
2024 // and defs at the end of the basic block. This is done as part of a
2025 // backwards walk, so defs should be processed before uses.
2026 void
2027 early_ra::record_artificial_refs (unsigned int flags)
2028 {
2029 df_ref ref;
2030
2031 FOR_EACH_ARTIFICIAL_DEF (ref, m_current_bb->index)
2032 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags
2033 && IN_RANGE (DF_REF_REGNO (ref), V0_REGNUM, V31_REGNUM))
2034 record_fpr_def (DF_REF_REGNO (ref));
2035 m_current_point += 1;
2036
2037 FOR_EACH_ARTIFICIAL_USE (ref, m_current_bb->index)
2038 if ((DF_REF_FLAGS (ref) & DF_REF_AT_TOP) == flags
2039 && IN_RANGE (DF_REF_REGNO (ref), V0_REGNUM, V31_REGNUM))
2040 record_fpr_use (DF_REF_REGNO (ref));
2041 m_current_point += 1;
2042 }
2043
2044 // Return true if:
2045 //
2046 // - X is a SUBREG, in which case it is a SUBREG of some REG Y
2047 //
2048 // - one 64-bit word of Y can be modified while preserving all other words
2049 //
2050 // - X refers to no more than one 64-bit word of Y
2051 //
2052 // - assigning FPRs to Y would put more than one 64-bit word in each FPR
2053 //
2054 // For example, this is true of:
2055 //
2056 // - (subreg:DI (reg:TI R) 0) and
2057 // - (subreg:DI (reg:TI R) 8)
2058 //
2059 // but is not true of:
2060 //
2061 // - (subreg:V2SI (reg:V2x2SI R) 0) or
2062 // - (subreg:V2SI (reg:V2x2SI R) 8).
2063 static bool
2064 allocno_assignment_is_rmw (rtx x)
2065 {
2066 if (partial_subreg_p (x))
2067 {
2068 auto outer_mode = GET_MODE (x);
2069 auto inner_mode = GET_MODE (SUBREG_REG (x));
2070 if (known_eq (REGMODE_NATURAL_SIZE (inner_mode), 0U + UNITS_PER_WORD)
2071 && known_lt (GET_MODE_SIZE (outer_mode), UNITS_PER_VREG))
2072 {
2073 auto nregs = targetm.hard_regno_nregs (V0_REGNUM, inner_mode);
2074 if (maybe_ne (nregs * UNITS_PER_WORD, GET_MODE_SIZE (inner_mode)))
2075 return true;
2076 }
2077 }
2078 return false;
2079 }
2080
2081 // Called as part of a backwards walk over a block. Model the definitions
2082 // in INSN, excluding partial call clobbers.
2083 void
2084 early_ra::record_insn_defs (rtx_insn *insn)
2085 {
2086 df_ref ref;
2087
2088 FOR_EACH_INSN_DEF (ref, insn)
2089 if (IN_RANGE (DF_REF_REGNO (ref), V0_REGNUM, V31_REGNUM))
2090 record_fpr_def (DF_REF_REGNO (ref));
2091 else
2092 {
2093 rtx reg = DF_REF_REG (ref);
2094 auto range = get_allocno_subgroup (reg);
2095 for (auto &allocno : range.allocnos ())
2096 {
2097 // Make sure that assigning to the DF_REF_REG clobbers the
2098 // whole of this allocno, not just some of it.
2099 if (allocno_assignment_is_rmw (reg))
2100 {
2101 record_live_range_failure ([&](){
2102 fprintf (dump_file, "read-modify-write of allocno %d",
2103 allocno.id);
2104 });
2105 break;
2106 }
2107
2108 // If the destination is unused, record a momentary blip
2109 // in its live range.
2110 if (!bitmap_bit_p (m_live_allocnos, allocno.id))
2111 record_allocno_use (&allocno);
2112 record_allocno_def (&allocno);
2113 }
2114 }
2115 m_current_point += 1;
2116 }
2117
2118 // Called as part of a backwards walk over a block. Model the call made
2119 // by INSN as a separate phase in the evaluation of the insn. Any partial
2120 // call clobbers happen at that point, rather than in the definition or use
2121 // phase of the insn.
2122 void
2123 early_ra::record_insn_call (rtx_call_insn *insn)
2124 {
2125 function_abi abi = insn_callee_abi (insn);
2126 m_call_points[abi.id ()].safe_push (m_current_point);
2127 m_current_point += 1;
2128 }
2129
2130 // Called as part of a backwards walk over a block. Model the uses in INSN.
2131 // We can ignore READ_MODIFY_WRITE uses of plain subregs, since we track the
2132 // FPR-sized parts of them individually.
2133 void
2134 early_ra::record_insn_uses (rtx_insn *insn)
2135 {
2136 df_ref ref;
2137
2138 FOR_EACH_INSN_USE (ref, insn)
2139 if (IN_RANGE (DF_REF_REGNO (ref), V0_REGNUM, V31_REGNUM))
2140 record_fpr_use (DF_REF_REGNO (ref));
2141 else if (!DF_REF_FLAGS_IS_SET (ref, DF_REF_READ_WRITE)
2142 || DF_REF_FLAGS_IS_SET (ref, DF_REF_STRICT_LOW_PART)
2143 || DF_REF_FLAGS_IS_SET (ref, DF_REF_ZERO_EXTRACT))
2144 {
2145 auto range = get_allocno_subgroup (DF_REF_REG (ref));
2146 for (auto &allocno : range.allocnos ())
2147 record_allocno_use (&allocno);
2148 }
2149 m_current_point += 1;
2150 }
2151
2152 // ALLOCNO->is_strong_copy_src is true. See whether ALLOCNO heads a
2153 // natural chain that has an affinity with the same hard register at
2154 // both ends.
2155 bool
2156 early_ra::consider_strong_copy_src_chain (allocno_info *allocno)
2157 {
2158 auto *src_allocno = allocno;
2159 while (src_allocno->copy_dest != INVALID_ALLOCNO)
2160 {
2161 auto *dest_allocno = m_allocnos[src_allocno->copy_dest];
2162 if (dest_allocno->start_point > src_allocno->end_point
2163 || dest_allocno->hard_regno != src_allocno->hard_regno)
2164 return false;
2165 gcc_checking_assert (dest_allocno->is_copy_dest);
2166 src_allocno = dest_allocno;
2167 }
2168
2169 while (allocno->copy_dest != INVALID_ALLOCNO)
2170 {
2171 allocno->is_strong_copy_src = 1;
2172 allocno = m_allocnos[allocno->copy_dest];
2173 allocno->is_strong_copy_dest = 1;
2174 }
2175 return true;
2176 }
2177
2178 // ALLOCNO1 and ALLOCNO2 are linked in some way, and might end up being
2179 // chained together. See whether chaining them requires the containing
2180 // groups to have the same stride, or whether it requires them to have
2181 // different strides. Return 1 if they should have the same stride,
2182 // -1 if they should have different strides, or 0 if it doesn't matter.
2183 int
2184 early_ra::strided_polarity_pref (allocno_info *allocno1,
2185 allocno_info *allocno2)
2186 {
2187 if (allocno1->offset + 1 < allocno1->group_size
2188 && allocno2->offset + 1 < allocno2->group_size)
2189 {
2190 if (is_chain_candidate (allocno1 + 1, allocno2 + 1, ALL_REASONS))
2191 return 1;
2192 else
2193 return -1;
2194 }
2195
2196 if (allocno1->offset > 0 && allocno2->offset > 0)
2197 {
2198 if (is_chain_candidate (allocno1 - 1, allocno2 - 1, ALL_REASONS))
2199 return 1;
2200 else
2201 return -1;
2202 }
2203
2204 return 0;
2205 }
2206
2207 // Decide which groups should be strided. Also complete "strong" copy chains.
2208 void
2209 early_ra::find_strided_accesses ()
2210 {
2211 // This function forms a graph of allocnos, linked by equivalences and
2212 // natural copy chains. It temporarily uses chain_next to record the
2213 // reverse of equivalence edges (related_allocno) and chain_prev to record
2214 // the reverse of copy edges (copy_dest).
2215 unsigned int allocno_info::*links[] = {
2216 &allocno_info::chain_next,
2217 &allocno_info::chain_prev,
2218 &allocno_info::copy_dest,
2219 &allocno_info::related_allocno
2220 };
2221
2222 // Set up the temporary reverse edges. Check for strong copy chains.
2223 for (unsigned int i = m_allocnos.length (); i-- > 0; )
2224 {
2225 auto *allocno1 = m_allocnos[i];
2226 if (allocno1->copy_dest != INVALID_ALLOCNO)
2227 m_allocnos[allocno1->copy_dest]->chain_prev = allocno1->id;
2228 if (allocno1->related_allocno != INVALID_ALLOCNO)
2229 m_allocnos[allocno1->related_allocno]->chain_next = allocno1->id;
2230
2231 if (allocno1->is_strong_copy_src
2232 && !allocno1->is_copy_dest
2233 && !consider_strong_copy_src_chain (allocno1))
2234 allocno1->is_strong_copy_src = false;
2235 }
2236
2237 // Partition the graph into cliques based on edges that have the following
2238 // properties:
2239 //
2240 // - the edge joins two allocnos whose groups have a free choice between
2241 // consecutive and strided allocations.
2242 //
2243 // - the two groups have a relative strided polarity preference (that is
2244 // they should make the same choice between consecutive and strided,
2245 // or they should make opposite choices).
2246 //
2247 // Assign relative polarities to each group connected in this way.
2248 //
2249 // The aim is to discover natural move-free striding choices, which will
2250 // often exist in carefully written ACLE code.
2251 unsigned int num_edges = m_allocnos.length () * ARRAY_SIZE (links);
2252 auto_sbitmap visited_edges (num_edges);
2253 bitmap_clear (visited_edges);
2254
2255 auto_vec<unsigned int, 32> worklist;
2256 for (unsigned int i = 0; i < num_edges; ++i)
2257 {
2258 if (!bitmap_set_bit (visited_edges, i))
2259 continue;
2260 worklist.quick_push (i);
2261 while (!worklist.is_empty ())
2262 {
2263 auto ei = worklist.pop ();
2264 auto *allocno1 = m_allocnos[ei / ARRAY_SIZE (links)];
2265 auto ai2 = allocno1->*links[ei % ARRAY_SIZE (links)];
2266 if (ai2 == INVALID_ALLOCNO)
2267 continue;
2268
2269 auto *allocno2 = m_allocnos[ai2];
2270 auto *group1 = allocno1->group ();
2271 auto *group2 = allocno2->group ();
2272 if (!group1->has_flexible_stride || !group2->has_flexible_stride)
2273 continue;
2274
2275 int pref = strided_polarity_pref (allocno1, allocno2);
2276 if (pref == 0)
2277 continue;
2278
2279 for (auto *group : { group1, group2 })
2280 for (auto &allocno : group->allocnos ())
2281 for (unsigned int j = 0; j < ARRAY_SIZE (links); ++j)
2282 if (bitmap_set_bit (visited_edges, allocno.id * 4 + j))
2283 worklist.safe_push (allocno.id * 4 + j);
2284
2285 if (group1->strided_polarity)
2286 group2->strided_polarity = group1->strided_polarity * pref;
2287 else if (group2->strided_polarity)
2288 group1->strided_polarity = group2->strided_polarity * pref;
2289 else
2290 {
2291 group1->strided_polarity = 1;
2292 group2->strided_polarity = pref;
2293 }
2294 }
2295 }
2296
2297 // Now look for edges between allocnos in multi-register groups where:
2298 //
2299 // - the two groups have a relative strided polarity preference (as above).
2300 //
2301 // - one group (G1) has a free choice between consecutive and strided
2302 // allocations.
2303 //
2304 // - the other group (G2) must use consecutive allocations.
2305 //
2306 // Update G1's individual preference for strided or consecutive allocations
2307 // based on G2. If the previous loop chose a polarity for G1, work out
2308 // whether it is better for polarity 1 or -1 to correspond to consecutive
2309 // allocation.
2310 int consecutive_pref = 0;
2311 for (unsigned int i = m_allocnos.length (); i-- > 0; )
2312 {
2313 auto *allocno1 = m_allocnos[i];
2314 for (auto link : links)
2315 {
2316 auto ai2 = allocno1->*link;
2317 if (ai2 == INVALID_ALLOCNO)
2318 continue;
2319
2320 auto *allocno2 = m_allocnos[ai2];
2321 auto *group1 = allocno1->group ();
2322 auto *group2 = allocno2->group ();
2323 if (group1->has_flexible_stride == group2->has_flexible_stride)
2324 continue;
2325
2326 int pref = strided_polarity_pref (allocno1, allocno2);
2327 if (pref == 0)
2328 continue;
2329
2330 auto *group = (group1->has_flexible_stride ? group1 : group2);
2331 consecutive_pref += group->strided_polarity * pref;
2332 group->consecutive_pref += pref;
2333 }
2334 }
2335
2336 // If it doesn't matter whether polarity 1 or -1 corresponds to consecutive
2337 // allocation, arbitrarily pick 1.
2338 if (consecutive_pref == 0)
2339 consecutive_pref = 1;
2340
2341 // Record which multi-register groups should use strided allocations.
2342 // Clear out the temporary edges.
2343 for (unsigned int ai = 0; ai < m_allocnos.length (); ++ai)
2344 {
2345 auto *allocno = m_allocnos[ai];
2346 allocno->chain_prev = INVALID_ALLOCNO;
2347 allocno->chain_next = INVALID_ALLOCNO;
2348
2349 if (allocno->offset != 0)
2350 continue;
2351
2352 auto *group = allocno->group ();
2353 if (!group->has_flexible_stride)
2354 continue;
2355
2356 bool make_strided = (group->strided_polarity
2357 ? (consecutive_pref * group->strided_polarity) < 0
2358 : group->consecutive_pref < 0);
2359 if (dump_file && (dump_flags & TDF_DETAILS))
2360 fprintf (dump_file, "Allocno [%d:%d]: strided polarity %d,"
2361 " consecutive pref %d, %s\n",
2362 allocno->id, allocno->id + group->size - 1,
2363 group->strided_polarity, group->consecutive_pref,
2364 make_strided ? "making strided" : "keeping consecutive");
2365 if (!make_strided)
2366 continue;
2367
2368 // 2-register groups have a stride of 8 FPRs and must start in
2369 // registers matching the mask 0x17. 4-register groups have a stride
2370 // of 4 FPRs and must start in registers matching the mask 0x13.
2371 group->stride = group->size == 2 ? 8 : 4;
2372 gcc_checking_assert (group->fpr_candidates
2373 == (group->size == 2 ? 0x55555555 : 0x11111111));
2374 group->fpr_candidates = (group->size == 2 ? 0xff00ff : 0xf000f);
2375 }
2376 }
2377
2378 // Compare the allocnos at *ALLOCNO1_PTR and *ALLOCNO2_PTR and return a <=>
2379 // result that puts allocnos in order of increasing FIELD.
2380 template<unsigned int early_ra::allocno_info::*field>
2381 int
2382 early_ra::cmp_increasing (const void *allocno1_ptr, const void *allocno2_ptr)
2383 {
2384 auto *allocno1 = *(allocno_info *const *) allocno1_ptr;
2385 auto *allocno2 = *(allocno_info *const *) allocno2_ptr;
2386
2387 if (allocno1->*field != allocno2->*field)
2388 return allocno1->*field < allocno2->*field ? -1 : 1;
2389 return (allocno1->id < allocno2->id ? -1
2390 : allocno1->id == allocno2->id ? 0 : 1);
2391 }
2392
2393 // Return true if we should consider chaining ALLOCNO1 onto the head
2394 // of ALLOCNO2. STRICTNESS says whether we should take copy-elision
2395 // heuristics into account, or whether we should just consider things
2396 // that matter for correctness.
2397 //
2398 // This is just a local test of the two allocnos; it doesn't guarantee
2399 // that chaining them would give a self-consistent system.
2400 bool
2401 early_ra::is_chain_candidate (allocno_info *allocno1, allocno_info *allocno2,
2402 test_strictness strictness)
2403 {
2404 if (allocno2->is_shared ())
2405 return false;
2406
2407 while (allocno1->is_equiv)
2408 allocno1 = m_allocnos[allocno1->related_allocno];
2409
2410 if (allocno2->start_point >= allocno1->end_point
2411 && !allocno2->is_equiv_to (allocno1->id))
2412 return false;
2413
2414 if (allocno1->is_earlyclobbered
2415 && allocno1->end_point == allocno2->start_point + 1)
2416 return false;
2417
2418 if (strictness == ALL_REASONS && allocno2->is_copy_dest)
2419 {
2420 if (allocno1->copy_dest != allocno2->id)
2421 return false;
2422 if (allocno2->is_strong_copy_dest && !allocno1->is_strong_copy_src)
2423 return false;
2424 }
2425 return true;
2426 }
2427
2428 // We're trying to chain allocno ALLOCNO1 to a later allocno.
2429 // Rate how good a choice ALLOCNO2 would be, with higher being better.
2430 int
2431 early_ra::rate_chain (allocno_info *allocno1, allocno_info *allocno2)
2432 {
2433 int score = 0;
2434 if (allocno2->is_strong_copy_dest)
2435 score += 256;
2436 else if (allocno2->is_copy_dest)
2437 score += 128;
2438
2439 // Prefer well-aligned matches.
2440 auto *group1 = allocno1->group ();
2441 auto *group2 = allocno2->group ();
2442 if (group1->stride == 1 && group2->stride == 1)
2443 {
2444 unsigned int min_size = std::min (group1->color_rep ()->size,
2445 group2->color_rep ()->size);
2446 if ((group1->color_rep_offset + allocno1->offset) % min_size
2447 == (group2->color_rep_offset + allocno2->offset) % min_size)
2448 score += min_size;
2449 else
2450 score -= min_size;
2451 }
2452 return score;
2453 }
2454
2455 // Sort the chain_candidate_infos at ARG1 and ARG2 in order of decreasing
2456 // score.
2457 int
2458 early_ra::cmp_chain_candidates (const void *arg1, const void *arg2)
2459 {
2460 auto &candidate1 = *(const chain_candidate_info *) arg1;
2461 auto &candidate2 = *(const chain_candidate_info *) arg2;
2462 if (candidate1.score != candidate2.score)
2463 return candidate1.score > candidate2.score ? -1 : 1;
2464
2465 // Prefer to increase the gap between uses of the allocated register,
2466 // to give the scheduler more freedom.
2467 auto *allocno1 = candidate1.allocno;
2468 auto *allocno2 = candidate2.allocno;
2469 if (allocno1->start_point != allocno2->start_point)
2470 return allocno1->start_point < allocno2->start_point ? -1 : 1;
2471
2472 if (allocno1 != allocno2)
2473 return allocno1->id < allocno2->id ? -1 : 1;
2474
2475 return 0;
2476 }
2477
2478 // Join the chains of allocnos that start at HEADI1 and HEADI2.
2479 // HEADI1 is either empty or a single allocno.
2480 void
2481 early_ra::chain_allocnos (unsigned int &headi1, unsigned int &headi2)
2482 {
2483 if (headi1 == INVALID_ALLOCNO)
2484 headi1 = headi2;
2485 else if (headi2 == INVALID_ALLOCNO)
2486 headi2 = headi1;
2487 else
2488 {
2489 auto *head1 = m_allocnos[headi1];
2490 auto *head2 = m_allocnos[headi2];
2491 gcc_checking_assert (head1->chain_next == INVALID_ALLOCNO
2492 && head1->chain_prev == INVALID_ALLOCNO
2493 && head2->chain_prev == INVALID_ALLOCNO);
2494
2495 if (head1->is_equiv
2496 && m_allocnos[head1->related_allocno]->copy_dest == headi2)
2497 {
2498 head1->is_copy_dest = head2->is_copy_dest;
2499 head1->is_strong_copy_dest = head2->is_strong_copy_dest;
2500 m_allocnos[head1->related_allocno]->copy_dest = headi1;
2501 }
2502 head1->chain_next = headi2;
2503 head2->chain_prev = headi1;
2504
2505 headi2 = headi1;
2506 }
2507 }
2508
2509 // Add GROUP2's FPR information to GROUP1's, given that GROUP2 starts
2510 // OFFSET allocnos into GROUP2.
2511 void
2512 early_ra::merge_fpr_info (allocno_group_info *group1,
2513 allocno_group_info *group2,
2514 unsigned int offset)
2515 {
2516 group1->fpr_size = std::max (group1->fpr_size, group2->fpr_size);
2517 group1->fpr_candidates &= (group2->fpr_candidates
2518 >> (offset * group1->stride));
2519 }
2520
2521 // Set the color representative of ALLOCNO's group to REP, such that ALLOCNO
2522 // ends being at allocno offset REP_OFFSET from the start of REP.
2523 void
2524 early_ra::set_single_color_rep (allocno_info *allocno, allocno_group_info *rep,
2525 unsigned int rep_offset)
2526 {
2527 auto *group = allocno->group ();
2528 if (group->m_color_rep == rep)
2529 return;
2530
2531 group->m_color_rep = rep;
2532 gcc_checking_assert (multiple_p (group->stride, rep->stride));
2533 unsigned int factor = group->stride / rep->stride;
2534 gcc_checking_assert (rep_offset >= allocno->offset * factor);
2535 group->color_rep_offset = rep_offset - allocno->offset * factor;
2536 merge_fpr_info (rep, group, group->color_rep_offset);
2537 }
2538
2539 // REP1 and REP2 are color representatives. Change REP1's color representative
2540 // to REP2, with REP1 starting at allocno offset REP2_OFFSET into REP2.
2541 void
2542 early_ra::set_color_rep (allocno_group_info *rep1, allocno_group_info *rep2,
2543 unsigned int rep2_offset)
2544 {
2545 gcc_checking_assert (rep1 != rep2
2546 && rep2->m_color_rep == rep2
2547 && multiple_p (rep1->stride, rep2->stride));
2548
2549 auto heads1 = rep1->chain_heads ();
2550 auto heads2 = rep2->chain_heads ();
2551 for (unsigned int i1 = 0; i1 < heads1.size (); ++i1)
2552 if (heads1[i1] != INVALID_ALLOCNO)
2553 {
2554 unsigned int i2 = rep2_offset + i1 * rep1->stride / rep2->stride;
2555 if (heads2[i2] == INVALID_ALLOCNO)
2556 heads2[i2] = heads1[i1];
2557 else
2558 gcc_checking_assert (heads2[i2] == heads1[i1]);
2559 set_single_color_rep (m_allocnos[heads1[i1]], rep2, i2);
2560 }
2561 }
2562
2563 // Try to chain ALLOCNO1 to the head of the chain starting at ALLOCNO2.
2564 // Return true on success.
2565 bool
2566 early_ra::try_to_chain_allocnos (allocno_info *allocno1,
2567 allocno_info *allocno2)
2568 {
2569 auto *group1 = allocno1->group ()->color_rep ();
2570 auto *group2 = allocno2->group ()->color_rep ();
2571
2572 // Avoid trying to tie different subgroups of the same group. This can
2573 // happen if the parts of a register are defined and used piecemeal.
2574 if (group1 == group2)
2575 return false;
2576
2577 // The stride (in FPRs) between allocnos of each color representative.
2578 auto fpr_stride1 = group1->stride;
2579 auto fpr_stride2 = group2->stride;
2580
2581 // The offset (in FPRs) of each allocno group from its color representative.
2582 auto fpr_offset1 = allocno1->group ()->color_rep_offset * fpr_stride1;
2583 auto fpr_offset2 = allocno2->group ()->color_rep_offset * fpr_stride2;
2584
2585 // The offset (in FPRs) of each allocno from its color representative.
2586 fpr_offset1 += allocno1->offset * allocno1->group ()->stride;
2587 fpr_offset2 += allocno2->offset * allocno2->group ()->stride;
2588
2589 // The FPR overlap is in multiples of the larger stride.
2590 auto max_fpr_stride = std::max (fpr_stride1, fpr_stride2);
2591 auto min_fpr_offset = std::min (fpr_offset1, fpr_offset2);
2592 auto fpr_overlap_offset = ROUND_DOWN (min_fpr_offset, max_fpr_stride);
2593
2594 // The offset (in FPRs) of the start of the overlapping region from
2595 // each color representative.
2596 fpr_offset1 -= fpr_overlap_offset;
2597 fpr_offset2 -= fpr_overlap_offset;
2598
2599 // The number of FPRs in each color representative after the start
2600 // of the overlapping region.
2601 auto fpr_after1 = (group1->size - 1) * fpr_stride1 - fpr_offset1;
2602 auto fpr_after2 = (group2->size - 1) * fpr_stride2 - fpr_offset2;
2603
2604 auto min_fpr_after = std::min (fpr_after1, fpr_after2);
2605
2606 // The number of overlapping allocnos.
2607 auto allocno_overlap_size = min_fpr_after / max_fpr_stride + 1;
2608
2609 // The offset (in allocnos) of the overlapping region from the start
2610 // of each color representative.
2611 auto allocno_offset1 = fpr_offset1 / fpr_stride1;
2612 auto allocno_offset2 = fpr_offset2 / fpr_stride2;
2613
2614 // The stride (in allocnos) between overlapping allocnos.
2615 auto allocno_stride1 = max_fpr_stride / fpr_stride1;
2616 auto allocno_stride2 = max_fpr_stride / fpr_stride2;
2617
2618 // Reject combinations that are impossible to allocate.
2619 auto fprs1 = group1->fpr_candidates;
2620 auto fprs2 = group2->fpr_candidates;
2621 if (fpr_offset1 > fpr_offset2)
2622 fprs2 >>= (fpr_offset1 - fpr_offset2);
2623 else
2624 fprs1 >>= (fpr_offset2 - fpr_offset1);
2625 if ((fprs1 & fprs2) == 0)
2626 {
2627 if (dump_file && (dump_flags & TDF_DETAILS))
2628 fprintf (dump_file, " - cannot chain %d->%d, no FPRs in common"
2629 " (%08x@%d and %08x@%d)\n", allocno1->id, allocno2->id,
2630 group1->fpr_candidates, fpr_offset1,
2631 group2->fpr_candidates, fpr_offset2);
2632 return false;
2633 }
2634
2635 // Check whether the chain can be formed.
2636 auto heads1 = group1->chain_heads ();
2637 auto heads2 = group2->chain_heads ();
2638 for (unsigned int i = 0; i < allocno_overlap_size; ++i)
2639 {
2640 auto headi1 = heads1[allocno_offset1 + i * allocno_stride1];
2641 auto headi2 = heads2[allocno_offset2 + i * allocno_stride2];
2642 if (headi1 != INVALID_ALLOCNO && headi2 != INVALID_ALLOCNO)
2643 {
2644 auto *head1 = m_allocnos[headi1];
2645 auto *head2 = m_allocnos[headi2];
2646 if (head1->chain_next != INVALID_ALLOCNO)
2647 return false;
2648 if (!is_chain_candidate (head1, head2, CORRECTNESS_ONLY))
2649 return false;
2650 }
2651 }
2652
2653 if (dump_file && (dump_flags & TDF_DETAILS))
2654 {
2655 fprintf (dump_file, " - chaining allocnos [");
2656 for (unsigned int i = 0; i < allocno_overlap_size; ++i)
2657 fprintf (dump_file, "%s%d", i ? "," : "",
2658 heads1[allocno_offset1 + i * allocno_stride1]);
2659 fprintf (dump_file, "] and [");
2660 for (unsigned int i = 0; i < allocno_overlap_size; ++i)
2661 fprintf (dump_file, "%s%d", i ? "," : "",
2662 heads2[allocno_offset2 + i * allocno_stride2]);
2663 fprintf (dump_file, "]\n");
2664 }
2665
2666 // Chain the allocnos, updating the chain heads.
2667 for (unsigned int i = 0; i < allocno_overlap_size; ++i)
2668 chain_allocnos (heads1[allocno_offset1 + i * allocno_stride1],
2669 heads2[allocno_offset2 + i * allocno_stride2]);
2670
2671 // Pick a color representative for the merged groups.
2672 allocno_group_info *new_rep;
2673 if (allocno_offset1 == 0
2674 && group1->size == allocno_overlap_size * allocno_stride1
2675 && multiple_p (fpr_stride1, fpr_stride2))
2676 {
2677 // The first group fits within the second.
2678 set_color_rep (group1, group2, allocno_offset2);
2679 new_rep = group2;
2680 }
2681 else if (allocno_offset2 == 0
2682 && group2->size == allocno_overlap_size * allocno_stride2
2683 && multiple_p (fpr_stride2, fpr_stride1))
2684 {
2685 // The second group fits within the first.
2686 set_color_rep (group2, group1, allocno_offset1);
2687 new_rep = group1;
2688 }
2689 else
2690 {
2691 // We need a new group that is big enough to span both groups.
2692 // The new group always has an FPR stride of 1.
2693 auto max_fpr_offset = std::max (fpr_offset1, fpr_offset2);
2694 auto max_fpr_after = std::max (fpr_after1, fpr_after2);
2695 auto new_size = max_fpr_offset + max_fpr_after + 1;
2696 new_rep = create_allocno_group (INVALID_REGNUM, new_size);
2697
2698 set_color_rep (group1, new_rep, max_fpr_offset - fpr_offset1);
2699 set_color_rep (group2, new_rep, max_fpr_offset - fpr_offset2);
2700 }
2701
2702 if (dump_file && (dump_flags & TDF_DETAILS))
2703 {
2704 fprintf (dump_file, " - new frontier [");
2705 auto new_heads = new_rep->chain_heads ();
2706 for (unsigned int i = 0; i < new_heads.size (); ++i)
2707 {
2708 if (i)
2709 fprintf (dump_file, ",");
2710 if (new_heads[i] == INVALID_ALLOCNO)
2711 fprintf (dump_file, "-");
2712 else
2713 fprintf (dump_file, "%d", new_heads[i]);
2714 }
2715 fprintf (dump_file, "]\n");
2716 }
2717
2718 return true;
2719 }
2720
2721 // Create a color_info for color representative GROUP.
2722 void
2723 early_ra::create_color (allocno_group_info *group)
2724 {
2725 auto *color = region_allocate<color_info> ();
2726 color->id = m_colors.length ();
2727 color->hard_regno = FIRST_PSEUDO_REGISTER;
2728 color->group = group;
2729
2730 gcc_checking_assert (group->m_color_rep == group);
2731 group->has_color = true;
2732 group->color = m_colors.length ();
2733
2734 m_colors.safe_push (color);
2735 }
2736
2737 // Form allocnos into chains. Create colors for each resulting clique.
2738 void
2739 early_ra::form_chains ()
2740 {
2741 if (dump_file && (dump_flags & TDF_DETAILS))
2742 fprintf (dump_file, "\nChaining allocnos:\n");
2743
2744 // Record conflicts of hard register and ABI conflicts before the
2745 // forming of chains so chains have the updated candidates
2746 for (auto *allocno1 : m_allocnos)
2747 {
2748 // Record conflicts with direct uses for FPR hard registers.
2749 auto *group1 = allocno1->group ();
2750 for (unsigned int fpr = allocno1->offset; fpr < 32; ++fpr)
2751 if (fpr_conflicts_with_allocno_p (fpr, allocno1))
2752 group1->fpr_candidates &= ~(1U << (fpr - allocno1->offset));
2753
2754 // Record conflicts due to partially call-clobbered registers.
2755 // (Full clobbers are handled by the previous loop.)
2756 for (unsigned int abi_id = 0; abi_id < NUM_ABI_IDS; ++abi_id)
2757 if (call_in_range_p (abi_id, allocno1->start_point,
2758 allocno1->end_point))
2759 {
2760 auto fprs = partial_fpr_clobbers (abi_id, group1->fpr_size);
2761 group1->fpr_candidates &= ~fprs >> allocno1->offset;
2762 }
2763 if (allocno1->is_shared ())
2764 {
2765 auto *allocno2 = m_allocnos[allocno1->related_allocno];
2766 merge_fpr_info (allocno2->group (), group1, allocno2->offset);
2767 }
2768 }
2769
2770 // Perform (modified) interval graph coloring. First sort by
2771 // increasing start point.
2772 m_sorted_allocnos.reserve (m_allocnos.length ());
2773 m_sorted_allocnos.splice (m_allocnos);
2774 m_sorted_allocnos.qsort (cmp_increasing<&allocno_info::start_point>);
2775
2776 // During this phase, color representatives are only correct for
2777 // unprocessed allocno groups (where the color representative is
2778 // the group itself) and for groups that contain a current chain head.
2779 unsigned int ti = 0;
2780 auto_vec<chain_candidate_info> candidates;
2781 for (unsigned int hi = 0; hi < m_sorted_allocnos.length (); ++hi)
2782 {
2783 auto *allocno1 = m_sorted_allocnos[hi];
2784 if (allocno1->chain_next != INVALID_ALLOCNO)
2785 continue;
2786
2787 if (allocno1->is_shared ())
2788 {
2789 if (dump_file && (dump_flags & TDF_DETAILS))
2790 fprintf (dump_file, " Allocno %d shares the same hard register"
2791 " as allocno %d\n", allocno1->id,
2792 allocno1->related_allocno);
2793 m_shared_allocnos.safe_push (allocno1);
2794 continue;
2795 }
2796
2797 // Find earlier allocnos (in processing order) that could be chained
2798 // to this one.
2799 candidates.truncate (0);
2800 for (unsigned int sci = ti; sci < hi; ++sci)
2801 {
2802 auto *allocno2 = m_sorted_allocnos[sci];
2803 if (allocno2->chain_prev == INVALID_ALLOCNO)
2804 {
2805 if (!is_chain_candidate (allocno1, allocno2, ALL_REASONS))
2806 continue;
2807 chain_candidate_info candidate;
2808 candidate.allocno = allocno2;
2809 candidate.score = rate_chain (allocno1, allocno2);
2810 candidates.safe_push (candidate);
2811 }
2812 else if (sci == ti)
2813 ++ti;
2814 }
2815
2816 // Sort the candidates by decreasing score.
2817 candidates.qsort (cmp_chain_candidates);
2818 if (dump_file && (dump_flags & TDF_DETAILS))
2819 {
2820 fprintf (dump_file, " Chain candidates for %d:", allocno1->id);
2821 for (auto &candidate : candidates)
2822 fprintf (dump_file, " %d(%d)", candidate.allocno->id,
2823 candidate.score);
2824 fprintf (dump_file, "\n");
2825 }
2826
2827 // Pick the first candidate that works.
2828 for (auto &candidate : candidates)
2829 if (try_to_chain_allocnos (allocno1, candidate.allocno))
2830 break;
2831 }
2832
2833 // Create color_infos for each group. Make sure that each group's
2834 // color representative is up to date.
2835 for (unsigned int hi = m_sorted_allocnos.length (); hi-- > 0; )
2836 {
2837 auto *allocno = m_sorted_allocnos[hi];
2838 if (allocno->is_shared ())
2839 continue;
2840
2841 auto *rep = allocno->group ()->color_rep ();
2842 if (rep->has_color)
2843 continue;
2844
2845 create_color (rep);
2846 auto heads = rep->chain_heads ();
2847 for (unsigned int i = 0; i < heads.size (); ++i)
2848 {
2849 unsigned int ai = heads[i];
2850 while (ai != INVALID_ALLOCNO)
2851 {
2852 allocno = m_allocnos[ai];
2853 set_single_color_rep (allocno, rep, i * rep->stride);
2854 ai = allocno->chain_next;
2855 }
2856 }
2857 }
2858 }
2859
2860 // Return true if the given FPR (starting at 0) conflicts with allocno
2861 // ALLOCNO.
2862 bool
2863 early_ra::fpr_conflicts_with_allocno_p (unsigned int fpr,
2864 allocno_info *allocno)
2865 {
2866 auto &ranges = m_fpr_ranges[fpr];
2867 unsigned int start_i = 0;
2868 unsigned int end_i = ranges.length ();
2869 while (start_i < end_i)
2870 {
2871 unsigned int mid_i = (start_i + end_i) / 2;
2872 auto &range = ranges[mid_i];
2873 if (allocno->end_point > range.start_point)
2874 start_i = mid_i + 1;
2875 else if (allocno->start_point < range.end_point)
2876 end_i = mid_i;
2877 else
2878 {
2879 if (range.allocno != allocno->id)
2880 return true;
2881 // The FPR is equivalent to ALLOCNO for this particular range.
2882 // See whether ALLOCNO conflicts with a neighboring range.
2883 if (mid_i > 0
2884 && ranges[mid_i - 1].start_point >= allocno->end_point)
2885 return true;
2886 if (mid_i + 1 < ranges.length ()
2887 && ranges[mid_i + 1].end_point <= allocno->start_point)
2888 return true;
2889 return false;
2890 }
2891 }
2892 return false;
2893 }
2894
2895 // Return true if there is a call with ABI identifier ABI_ID in the inclusive
2896 // program point range [START_POINT, END_POINT].
2897 bool
2898 early_ra::call_in_range_p (unsigned int abi_id, unsigned int start_point,
2899 unsigned int end_point)
2900 {
2901 auto &points = m_call_points[abi_id];
2902 unsigned int start_i = 0;
2903 unsigned int end_i = points.length ();
2904 while (start_i < end_i)
2905 {
2906 unsigned int mid_i = (start_i + end_i) / 2;
2907 auto point = points[mid_i];
2908 if (end_point > point)
2909 start_i = mid_i + 1;
2910 else if (start_point < point)
2911 end_i = mid_i;
2912 else
2913 return true;
2914 }
2915 return false;
2916 }
2917
2918 // Return the set of FPRs for which a value of size SIZE will be clobbered
2919 // by a call to a function with ABI identifier ABI_ID, but would not be
2920 // for some smaller size. The set therefore excludes FPRs that are
2921 // fully-clobbered, like V0 in the base ABI.
2922 unsigned int
2923 early_ra::partial_fpr_clobbers (unsigned int abi_id, fpr_size_info size)
2924 {
2925 auto &abi = function_abis[abi_id];
2926 unsigned int clobbers = 0;
2927 machine_mode mode = (size == FPR_D ? V8QImode
2928 : size == FPR_Q ? V16QImode : VNx16QImode);
2929 for (unsigned int regno = V0_REGNUM; regno <= V31_REGNUM; ++regno)
2930 if (!abi.clobbers_full_reg_p (regno)
2931 && abi.clobbers_reg_p (mode, regno))
2932 clobbers |= 1U << (regno - V0_REGNUM);
2933 return clobbers;
2934 }
2935
2936 // Process copies between pseudo registers and hard registers and update
2937 // the FPR preferences for the associated colors.
2938 void
2939 early_ra::process_copies ()
2940 {
2941 for (auto &copy : m_allocno_copies)
2942 {
2943 auto *allocno = m_allocnos[copy.allocno];
2944 auto *group = allocno->group ();
2945 auto offset = group->color_rep_offset + allocno->offset;
2946 if (offset > copy.fpr)
2947 continue;
2948
2949 unsigned int fpr = copy.fpr - offset;
2950 auto *color = m_colors[group->color_rep ()->color];
2951 color->fpr_preferences[fpr] = MIN (color->fpr_preferences[fpr]
2952 + copy.weight, 127);
2953 color->num_fpr_preferences += copy.weight;
2954 }
2955 }
2956
2957 // Compare the colors at *COLOR1_PTR and *COLOR2_PTR and return a <=>
2958 // result that puts colors in allocation order.
2959 int
2960 early_ra::cmp_allocation_order (const void *color1_ptr, const void *color2_ptr)
2961 {
2962 auto *color1 = *(color_info *const *) color1_ptr;
2963 auto *color2 = *(color_info *const *) color2_ptr;
2964
2965 // Allocate bigger groups before smaller groups.
2966 if (color1->group->size != color2->group->size)
2967 return color1->group->size > color2->group->size ? -1 : 1;
2968
2969 // Allocate groups with stronger FPR preferences before groups with weaker
2970 // FPR preferences.
2971 if (color1->num_fpr_preferences != color2->num_fpr_preferences)
2972 return color1->num_fpr_preferences > color2->num_fpr_preferences ? -1 : 1;
2973
2974 return (color1->id < color2->id ? -1
2975 : color1->id == color2->id ? 0 : 1);
2976 }
2977
2978 // Allocate a register to each color. If we run out of registers,
2979 // give up on doing a full allocation of the FPR-based pseudos in the
2980 // region.
2981 void
2982 early_ra::allocate_colors ()
2983 {
2984 if (dump_file && (dump_flags & TDF_DETAILS))
2985 fprintf (dump_file, "\nAllocating registers:\n");
2986
2987 auto_vec<color_info *> sorted_colors;
2988 sorted_colors.safe_splice (m_colors);
2989 sorted_colors.qsort (cmp_allocation_order);
2990
2991 for (unsigned int i = 0; i < 32; ++i)
2992 if (!crtl->abi->clobbers_full_reg_p (V0_REGNUM + i))
2993 m_call_preserved_fprs |= 1U << i;
2994
2995 for (auto *color : sorted_colors)
2996 {
2997 unsigned int candidates = color->group->fpr_candidates;
2998 for (unsigned int i = 0; i < color->group->size; ++i)
2999 candidates &= ~(m_allocated_fprs >> i);
3000 unsigned int best = INVALID_REGNUM;
3001 int best_weight = 0;
3002 unsigned int best_recency = 0;
3003 for (unsigned int fpr = 0; fpr <= 32U - color->group->size; ++fpr)
3004 {
3005 if ((candidates & (1U << fpr)) == 0)
3006 continue;
3007 int weight = color->fpr_preferences[fpr];
3008 unsigned int recency = 0;
3009 // Account for registers that the current function must preserve.
3010 for (unsigned int i = 0; i < color->group->size; ++i)
3011 {
3012 if (m_call_preserved_fprs & (1U << (fpr + i)))
3013 weight -= 1;
3014 recency = MAX (recency, m_fpr_recency[fpr + i]);
3015 }
3016 // Prefer higher-numbered registers in the event of a tie.
3017 // This should tend to keep lower-numbered registers free
3018 // for allocnos that require V0-V7 or V0-V15.
3019 if (best == INVALID_REGNUM
3020 || best_weight < weight
3021 || (best_weight == weight && recency <= best_recency))
3022 {
3023 best = fpr;
3024 best_weight = weight;
3025 best_recency = recency;
3026 }
3027 }
3028
3029 if (best == INVALID_REGNUM)
3030 {
3031 record_allocation_failure ([&](){
3032 fprintf (dump_file, "no free register for color %d", color->id);
3033 });
3034 return;
3035 }
3036
3037 color->hard_regno = best + V0_REGNUM;
3038 if (dump_file && (dump_flags & TDF_DETAILS))
3039 fprintf (dump_file, " Allocating [v%d:v%d] to color %d\n",
3040 best, best + color->group->size - 1, color->id);
3041 // Mark the COLOR's FPRs as allocated. A full-width color can have
3042 // size == 32, so shift a wide enough value: "1U << 32" is undefined,
3043 // as is "1UL << 32" on hosts with 32-bit long. unsigned long long is
3044 // at least 64 bits everywhere. best + size <= 32 (from the candidate
3045 // search) keeps the result within the 32-bit mask.
3046 gcc_assert (best + color->group->size <= 32);
3047 m_allocated_fprs |= ((1ULL << color->group->size) - 1) << best;
3048 }
3049 }
3050
3051 // See if ALLOCNO ends a subchain of single registers that can be split
3052 // off without affecting the rest of the chain, and without introducing
3053 // any moves. Return the start of the chain if so (which might be ALLOCNO
3054 // itself), otherwise return null.
3055 early_ra::allocno_info *
3056 early_ra::find_independent_subchain (allocno_info *allocno)
3057 {
3058 // Make sure ALLOCNO ends a natural subchain.
3059 if (auto *next_allocno = chain_next (allocno))
3060 if (next_allocno->start_point + 1 >= allocno->end_point)
3061 return nullptr;
3062
3063 // Check the allocnos in the purported subchain and find the other end.
3064 for (;;)
3065 {
3066 auto *group = allocno->group ();
3067 if (group->m_color_rep == group)
3068 return nullptr;
3069 if (group->size != 1)
3070 return nullptr;
3071
3072 auto *prev_allocno = chain_prev (allocno);
3073 if (!prev_allocno || allocno->start_point + 1 < prev_allocno->end_point)
3074 return allocno;
3075
3076 allocno = prev_allocno;
3077 }
3078 }
3079
3080 // Search the colors starting at index FIRST_COLOR whose FPRs do not belong
3081 // to FPR_CONFLICTS. Return the first such color that has no group. If all
3082 // such colors have groups, instead return the color with the latest
3083 // (smallest) start point.
3084 early_ra::color_info *
3085 early_ra::find_oldest_color (unsigned int first_color,
3086 unsigned int fpr_conflicts)
3087 {
3088 color_info *best = nullptr;
3089 unsigned int best_start_point = ~0U;
3090 unsigned int best_recency = 0;
3091 for (unsigned int ci = first_color; ci < m_colors.length (); ++ci)
3092 {
3093 auto *color = m_colors[ci];
3094 unsigned int fpr = color->hard_regno - V0_REGNUM;
3095 if (fpr_conflicts & (1U << fpr))
3096 continue;
3097 unsigned int start_point = 0;
3098 if (color->group)
3099 {
3100 auto chain_head = color->group->chain_heads ()[0];
3101 start_point = m_allocnos[chain_head]->start_point;
3102 }
3103 unsigned int recency = m_fpr_recency[fpr];
3104 if (!best
3105 || best_start_point > start_point
3106 || (best_start_point == start_point && recency < best_recency))
3107 {
3108 best = color;
3109 best_start_point = start_point;
3110 best_recency = recency;
3111 }
3112 }
3113 return best;
3114 }
3115
3116 // If there are some spare FPRs that can be reused without introducing saves,
3117 // restores, or moves, use them to "broaden" the allocation, in order to give
3118 // the scheduler more freedom. This is particularly useful for forming LDPs
3119 // and STPs.
3120 void
3121 early_ra::broaden_colors ()
3122 {
3123 // Create dummy colors for every leftover FPR that can be used cheaply.
3124 unsigned int first_color = m_colors.length ();
3125 for (unsigned int fpr = 0; fpr < 32; ++fpr)
3126 if (((m_allocated_fprs | m_call_preserved_fprs) & (1U << fpr)) == 0)
3127 {
3128 auto *color = region_allocate<color_info> ();
3129 color->id = m_colors.length ();
3130 color->hard_regno = V0_REGNUM + fpr;
3131 color->group = nullptr;
3132 m_colors.safe_push (color);
3133 }
3134
3135 // Exit early if there are no spare FPRs.
3136 if (first_color == m_colors.length ())
3137 return;
3138
3139 // Go through the allocnos in order, seeing if there is a subchain of
3140 // single-FPR allocnos that can be split off from the containingg clique.
3141 // Allocate such subchains to the new colors on an oldest-first basis.
3142 for (auto *allocno : m_sorted_allocnos)
3143 if (auto *start_allocno = find_independent_subchain (allocno))
3144 {
3145 unsigned int fpr_conflicts = 0;
3146 auto *member = allocno;
3147 for (;;)
3148 {
3149 fpr_conflicts |= ~member->group ()->fpr_candidates;
3150 if (member == start_allocno)
3151 break;
3152 member = m_allocnos[member->chain_prev];
3153 }
3154
3155 auto *color = find_oldest_color (first_color, fpr_conflicts);
3156 if (!color)
3157 continue;
3158
3159 if (!color->group)
3160 {
3161 auto *group = allocno->group ();
3162 color->group = group;
3163 group->color = color->id;
3164 group->chain_heads ()[0] = INVALID_ALLOCNO;
3165 }
3166 else
3167 {
3168 auto chain_head = color->group->chain_heads ()[0];
3169 auto start_point = m_allocnos[chain_head]->start_point;
3170 if (start_point >= allocno->end_point)
3171 // Allocating to COLOR isn't viable, and it was the best
3172 // option available.
3173 continue;
3174
3175 auto *next_allocno = chain_next (allocno);
3176 if (!next_allocno || next_allocno->start_point <= start_point)
3177 // The current allocation gives at least as much scheduling
3178 // freedom as COLOR would.
3179 continue;
3180 }
3181
3182 // Unlink the chain.
3183 if (auto *next_allocno = chain_next (allocno))
3184 next_allocno->chain_prev = start_allocno->chain_prev;
3185 if (auto *prev_allocno = chain_prev (start_allocno))
3186 prev_allocno->chain_next = allocno->chain_next;
3187
3188 // Make the subchain use COLOR.
3189 allocno->chain_next = color->group->chain_heads ()[0];
3190 if (dump_file && (dump_flags & TDF_DETAILS))
3191 fprintf (dump_file, "Moving to optional color %d (register %s):",
3192 color->id, reg_names[color->hard_regno]);
3193 for (;;)
3194 {
3195 auto *group = allocno->group ();
3196 if (dump_file && (dump_flags & TDF_DETAILS))
3197 fprintf (dump_file, " r%d", group->regno);
3198 group->m_color_rep = color->group;
3199 group->color_rep_offset = 0;
3200 if (allocno == start_allocno)
3201 break;
3202 allocno = m_allocnos[allocno->chain_prev];
3203 }
3204 if (dump_file && (dump_flags & TDF_DETAILS))
3205 fprintf (dump_file, "\n");
3206 color->group->chain_heads ()[0] = start_allocno->id;
3207 }
3208 }
3209
3210 // Record the final choice of hard register for each allocno.
3211 void
3212 early_ra::finalize_allocation ()
3213 {
3214 for (auto *color : m_colors)
3215 if (color->group)
3216 {
3217 unsigned int fpr = color->hard_regno - V0_REGNUM;
3218 for (unsigned int i = 0; i < color->group->size; ++i)
3219 m_fpr_recency[fpr + i] = m_current_region;
3220 }
3221 for (auto *allocno : m_allocnos)
3222 {
3223 if (allocno->is_shared ())
3224 continue;
3225 auto *group = allocno->group ();
3226 auto *rep = group->color_rep ();
3227 auto rep_regno = m_colors[rep->color]->hard_regno;
3228 auto group_regno = rep_regno + group->color_rep_offset;
3229 allocno->hard_regno = group_regno + allocno->offset * group->stride;
3230 }
3231 for (auto *allocno : m_shared_allocnos)
3232 allocno->hard_regno = m_allocnos[allocno->related_allocno]->hard_regno;
3233 }
3234
3235 // Replace any allocno references in REFS with the allocated register.
3236 // INSN is the instruction that contains REFS.
3237 bool
3238 early_ra::replace_regs (rtx_insn *insn, df_ref refs)
3239 {
3240 bool changed = false;
3241 for (df_ref ref = refs; ref; ref = DF_REF_NEXT_LOC (ref))
3242 {
3243 auto range = get_allocno_subgroup (DF_REF_REG (ref));
3244 if (!range)
3245 continue;
3246
3247 auto new_regno = range.allocno (0)->hard_regno;
3248 if (new_regno == FIRST_PSEUDO_REGISTER)
3249 {
3250 // Reset a debug instruction if, after DCE, the only remaining
3251 // references to a register are in such instructions.
3252 gcc_assert (DEBUG_INSN_P (insn));
3253 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3254 return true;
3255 }
3256 *DF_REF_LOC (ref) = gen_rtx_REG (GET_MODE (DF_REF_REG (ref)), new_regno);
3257 changed = true;
3258 }
3259 return changed;
3260 }
3261
3262 // Try to make INSN match its FPR-related constraints. If this needs
3263 // a source operand (SRC) to be copied to a destination operand (DEST)
3264 // before INSN, add the associated (DEST, SRC) pairs to MOVES.
3265 //
3266 // Return -1 on failure, otherwise return a ?/!-style reject count.
3267 // The reject count doesn't model the moves, just the internal alternative
3268 // preferences.
3269 int
3270 early_ra::try_enforce_constraints (rtx_insn *insn,
3271 vec<std::pair<int, int>> &moves)
3272 {
3273 if (!constrain_operands (0, get_preferred_alternatives (insn)))
3274 return -1;
3275
3276 // Pick the alternative with the lowest cost.
3277 int best = -1;
3278 auto alts = get_preferred_alternatives (insn);
3279 for (int altno = 0; altno < recog_data.n_alternatives; ++altno)
3280 {
3281 if (!(alts & ALTERNATIVE_BIT (altno)))
3282 continue;
3283
3284 auto *op_alt = &recog_op_alt[altno * recog_data.n_operands];
3285 if (!likely_alternative_match_p (op_alt))
3286 continue;
3287
3288 auto_vec<std::pair<int, int>, 4> new_moves;
3289 for (int opno = 0; opno < recog_data.n_operands; ++opno)
3290 {
3291 rtx op = recog_data.operand[opno];
3292 if (REG_P (op)
3293 && FP_REGNUM_P (REGNO (op))
3294 && op_alt[opno].matched >= 0)
3295 {
3296 rtx old_src = recog_data.operand[op_alt[opno].matched];
3297 if (!operands_match_p (op, old_src))
3298 {
3299 for (int i = 0; i < recog_data.n_operands; ++i)
3300 if (i != opno)
3301 {
3302 rtx other = recog_data.operand[i];
3303 if (reg_overlap_mentioned_p (op, other))
3304 {
3305 old_src = NULL_RTX;
3306 break;
3307 }
3308 }
3309 if (!old_src)
3310 continue;
3311 new_moves.safe_push ({ opno, op_alt[opno].matched });
3312 }
3313 }
3314 }
3315 int cost = count_rejects (op_alt) + new_moves.length () * 7;
3316 if (best < 0 || cost < best)
3317 {
3318 best = cost;
3319 moves.truncate (0);
3320 moves.safe_splice (new_moves);
3321 }
3322 }
3323 return best;
3324 }
3325
3326 // Make INSN matches its FPR-related constraints.
3327 void
3328 early_ra::enforce_constraints (rtx_insn *insn)
3329 {
3330 extract_insn (insn);
3331 preprocess_constraints (insn);
3332
3333 // First try with the operands they are.
3334 auto_vec<std::pair<int, int>, 4> moves;
3335 int cost = try_enforce_constraints (insn, moves);
3336
3337 // Next try taking advantage of commutativity.
3338 for (int opno = 0; opno < recog_data.n_operands - 1; ++opno)
3339 if (recog_data.constraints[opno][0] == '%')
3340 {
3341 std::swap (*recog_data.operand_loc[opno],
3342 *recog_data.operand_loc[opno + 1]);
3343 std::swap (recog_data.operand[opno],
3344 recog_data.operand[opno + 1]);
3345 auto_vec<std::pair<int, int>, 4> swapped_moves;
3346 int swapped_cost = try_enforce_constraints (insn, swapped_moves);
3347 if (swapped_cost >= 0 && (cost < 0 || swapped_cost < cost))
3348 {
3349 cost = swapped_cost;
3350 moves.truncate (0);
3351 moves.safe_splice (swapped_moves);
3352 }
3353 else
3354 {
3355 std::swap (*recog_data.operand_loc[opno],
3356 *recog_data.operand_loc[opno + 1]);
3357 std::swap (recog_data.operand[opno],
3358 recog_data.operand[opno + 1]);
3359 }
3360 }
3361
3362 // The allocation should ensure that there is at least one valid combination.
3363 // It's too late to back out now if not.
3364 gcc_assert (cost >= 0);
3365 for (int i = 0; i < recog_data.n_dups; ++i)
3366 {
3367 int dup_of = recog_data.dup_num[i];
3368 rtx new_op = *recog_data.operand_loc[dup_of];
3369 if (new_op != recog_data.operand[dup_of])
3370 *recog_data.dup_loc[i] = copy_rtx (new_op);
3371 }
3372 for (auto move : moves)
3373 {
3374 int dest_opno = move.first;
3375 int src_opno = move.second;
3376 rtx dest = recog_data.operand[dest_opno];
3377 rtx old_src = recog_data.operand[src_opno];
3378 rtx new_src = lowpart_subreg (GET_MODE (old_src), dest, GET_MODE (dest));
3379 emit_insn_before (gen_move_insn (new_src, old_src), insn);
3380 *recog_data.operand_loc[src_opno] = new_src;
3381 }
3382 }
3383
3384 // See whether INSN is an instruction that operates on multi-register vectors,
3385 // and if we have decided to make it use strided rather than consecutive
3386 // accesses. Update the pattern and return true if so.
3387 bool
3388 early_ra::maybe_convert_to_strided_access (rtx_insn *insn)
3389 {
3390 if (!NONJUMP_INSN_P (insn) || recog_memoized (insn) < 0)
3391 return false;
3392
3393 auto stride_type = get_attr_stride_type (insn);
3394 rtx pat = PATTERN (insn);
3395 rtx op;
3396 if (TARGET_STREAMING_SME2 && stride_type == STRIDE_TYPE_LD1_CONSECUTIVE)
3397 op = SET_DEST (pat);
3398 else if (TARGET_STREAMING_SME2 && stride_type == STRIDE_TYPE_ST1_CONSECUTIVE)
3399 op = XVECEXP (SET_SRC (pat), 0, 1);
3400 else
3401 return false;
3402
3403 auto range = get_allocno_subgroup (op);
3404 if (!range || range.group->stride == 1)
3405 return false;
3406
3407 gcc_assert (range.start == 0 && range.count == range.group->size);
3408 auto elt_mode = GET_MODE_INNER (GET_MODE (op));
3409 auto single_mode = aarch64_full_sve_mode (elt_mode).require ();
3410 auto_vec<rtx, 4> regs;
3411 for (unsigned int i = 0; i < range.count; ++i)
3412 regs.quick_push (gen_rtx_REG (single_mode, range.allocno (i)->hard_regno));
3413
3414 extract_insn (insn);
3415 if (stride_type == STRIDE_TYPE_LD1_CONSECUTIVE)
3416 {
3417 auto unspec = XINT (SET_SRC (pat), 1);
3418 if (range.count == 2)
3419 pat = gen_aarch64_strided2 (unspec, GET_MODE (op), regs[0], regs[1],
3420 recog_data.operand[1],
3421 recog_data.operand[2]);
3422 else
3423 pat = gen_aarch64_strided4 (unspec, GET_MODE (op),
3424 regs[0], regs[1], regs[2], regs[3],
3425 recog_data.operand[1],
3426 recog_data.operand[2]);
3427 }
3428 else if (stride_type == STRIDE_TYPE_ST1_CONSECUTIVE)
3429 {
3430 auto unspec = XINT (SET_SRC (pat), 1);
3431 if (range.count == 2)
3432 pat = gen_aarch64_strided2 (unspec, GET_MODE (op),
3433 recog_data.operand[0],
3434 recog_data.operand[2], regs[0], regs[1]);
3435 else
3436 pat = gen_aarch64_strided4 (unspec, GET_MODE (op),
3437 recog_data.operand[0],
3438 recog_data.operand[2],
3439 regs[0], regs[1], regs[2], regs[3]);
3440 // Ensure correct sharing for the source memory.
3441 //
3442 // ??? Why doesn't the generator get this right?
3443 XVECEXP (SET_SRC (pat), 0, XVECLEN (SET_SRC (pat), 0) - 1)
3444 = *recog_data.dup_loc[0];
3445 }
3446 else
3447 gcc_unreachable ();
3448 PATTERN (insn) = pat;
3449 INSN_CODE (insn) = -1;
3450 df_insn_rescan (insn);
3451 return true;
3452 }
3453
3454 // We've successfully allocated the current region. Apply the allocation
3455 // to the instructions.
3456 void
3457 early_ra::apply_allocation ()
3458 {
3459 for (auto *insn : m_dead_insns)
3460 set_insn_deleted (insn);
3461
3462 rtx_insn *prev;
3463 for (auto insn_range : m_insn_ranges)
3464 for (rtx_insn *insn = insn_range.first;
3465 insn != insn_range.second;
3466 insn = prev)
3467 {
3468 prev = PREV_INSN (insn);
3469 if (!INSN_P (insn))
3470 continue;
3471
3472 bool changed = maybe_convert_to_strided_access (insn);
3473 changed |= replace_regs (insn, DF_INSN_DEFS (insn));
3474 changed |= replace_regs (insn, DF_INSN_USES (insn));
3475 if (changed && NONDEBUG_INSN_P (insn))
3476 {
3477 if (GET_CODE (PATTERN (insn)) != USE
3478 && GET_CODE (PATTERN (insn)) != CLOBBER
3479 && !is_move_set (PATTERN (insn)))
3480 enforce_constraints (insn);
3481
3482 // A REG_EQUIV note establishes an equivalence throughout
3483 // the function, but here we're reusing hard registers for
3484 // multiple pseudo registers. We also no longer need REG_EQUIV
3485 // notes that record potential spill locations, since we've
3486 // allocated the pseudo register without spilling.
3487 rtx *ptr = &REG_NOTES (insn);
3488 while (*ptr)
3489 if (REG_NOTE_KIND (*ptr) == REG_EQUIV)
3490 *ptr = XEXP (*ptr, 1);
3491 else
3492 ptr = &XEXP (*ptr, 1);
3493 }
3494 changed |= replace_regs (insn, DF_INSN_EQ_USES (insn));
3495 if (changed)
3496 df_insn_rescan (insn);
3497 }
3498
3499 for (auto *insn : m_dead_insns)
3500 delete_insn (insn);
3501 }
3502
3503 // Try to allocate the current region. Update the instructions if successful.
3504 void
3505 early_ra::process_region ()
3506 {
3507 for (auto *allocno : m_allocnos)
3508 {
3509 allocno->chain_next = INVALID_ALLOCNO;
3510 allocno->chain_prev = INVALID_ALLOCNO;
3511 }
3512
3513 if (dump_file && (dump_flags & TDF_DETAILS))
3514 {
3515 dump_fpr_ranges ();
3516 dump_copies ();
3517 dump_allocnos ();
3518 }
3519
3520 find_strided_accesses ();
3521
3522 if (dump_file && (dump_flags & TDF_DETAILS))
3523 dump_allocnos ();
3524
3525 form_chains ();
3526
3527 if (dump_file && (dump_flags & TDF_DETAILS))
3528 dump_allocnos ();
3529
3530 process_copies ();
3531
3532 if (dump_file && (dump_flags & TDF_DETAILS))
3533 dump_colors ();
3534
3535 allocate_colors ();
3536 if (!m_allocation_successful)
3537 return;
3538
3539 broaden_colors ();
3540 finalize_allocation ();
3541
3542 if (dump_file && (dump_flags & TDF_DETAILS))
3543 {
3544 fprintf (dump_file, "\nAllocation successful\nFinal allocation:\n");
3545 dump_allocnos ();
3546 dump_colors ();
3547 }
3548
3549 apply_allocation ();
3550 }
3551
3552 // Return true if INSN would become dead if we successfully allocate the
3553 // current region.
3554 bool
3555 early_ra::is_dead_insn (rtx_insn *insn)
3556 {
3557 rtx set = single_set (insn);
3558 if (!set)
3559 return false;
3560
3561 rtx dest = SET_DEST (set);
3562 auto dest_range = get_allocno_subgroup (dest);
3563 if (!dest_range)
3564 return false;
3565
3566 for (auto &allocno : dest_range.allocnos ())
3567 if (bitmap_bit_p (m_live_allocnos, allocno.id))
3568 return false;
3569
3570 if (side_effects_p (set))
3571 return false;
3572
3573 /* If we can't delete dead exceptions and the insn throws,
3574 then the instruction is not dead. */
3575 if (!cfun->can_delete_dead_exceptions
3576 && !insn_nothrow_p (insn))
3577 return false;
3578
3579 return true;
3580 }
3581
3582 // Return true if we could split a single-block region into two regions
3583 // at the current program point. This is true if the block up to this
3584 // point is worth treating as an independent region and if no registers
3585 // that would affect the allocation are currently live.
3586 inline bool
3587 early_ra::could_split_region_here ()
3588 {
3589 return (m_accurate_live_ranges
3590 && bitmap_empty_p (m_live_allocnos)
3591 && m_live_fprs == 0
3592 && !m_allocnos.is_empty ());
3593 }
3594
3595 // Return true if any of the pseudos defined by INSN are also defined
3596 // elsewhere.
3597 static bool
3598 defines_multi_def_pseudo (rtx_insn *insn)
3599 {
3600 df_ref ref;
3601
3602 FOR_EACH_INSN_DEF (ref, insn)
3603 {
3604 unsigned int regno = DF_REF_REGNO (ref);
3605 if (!HARD_REGISTER_NUM_P (regno) && DF_REG_DEF_COUNT (regno) > 1)
3606 return true;
3607 }
3608 return false;
3609 }
3610
3611 // Build up information about block BB. IS_ISOLATED is true if the
3612 // block is not part of a larger region.
3613 void
3614 early_ra::process_block (basic_block bb, bool is_isolated)
3615 {
3616 m_current_bb = bb;
3617 m_current_point += 1;
3618 m_current_bb_point = m_current_point;
3619
3620 // Process live-out FPRs.
3621 bitmap live_out = df_get_live_out (bb);
3622 for (unsigned int regno = V0_REGNUM; regno <= V31_REGNUM; ++regno)
3623 if (bitmap_bit_p (live_out, regno))
3624 record_fpr_use (regno);
3625
3626 // Process live-out allocnos. We don't track individual FPR liveness
3627 // across block boundaries, so we have to assume that the whole pseudo
3628 // register is live.
3629 bitmap_iterator bi;
3630 unsigned int regno;
3631 EXECUTE_IF_AND_IN_BITMAP (df_get_live_out (bb), m_fpr_pseudos,
3632 FIRST_PSEUDO_REGISTER, regno, bi)
3633 {
3634 auto range = get_allocno_subgroup (regno_reg_rtx[regno]);
3635 for (auto &allocno : range.allocnos ())
3636 record_allocno_use (&allocno);
3637 }
3638
3639 m_current_point += 1;
3640
3641 record_artificial_refs (0);
3642
3643 bool is_first = true;
3644 rtx_insn *start_insn = BB_END (bb);
3645 rtx_insn *insn;
3646 FOR_BB_INSNS_REVERSE (bb, insn)
3647 {
3648 if (!NONDEBUG_INSN_P (insn))
3649 continue;
3650
3651 // CLOBBERs are used to prevent pseudos from being upwards exposed.
3652 // We can ignore them if allocation is successful.
3653 if (GET_CODE (PATTERN (insn)) == CLOBBER)
3654 {
3655 if (get_allocno_subgroup (XEXP (PATTERN (insn), 0)))
3656 m_dead_insns.safe_push (insn);
3657 continue;
3658 }
3659
3660 if (dump_file && (dump_flags & TDF_DETAILS))
3661 {
3662 if (is_first)
3663 fprintf (dump_file, "\nBlock %d:\n", bb->index);
3664 fprintf (dump_file, "%6d:", m_current_point);
3665 pretty_printer rtl_slim_pp;
3666 rtl_slim_pp.set_output_stream (dump_file);
3667 print_insn (&rtl_slim_pp, insn, 1);
3668 pp_flush (&rtl_slim_pp);
3669 fprintf (dump_file, "\n");
3670 }
3671 is_first = false;
3672
3673 if (is_dead_insn (insn))
3674 {
3675 if (dump_file && (dump_flags & TDF_DETAILS))
3676 fprintf (dump_file, "%14s -- dead\n", "");
3677 m_dead_insns.safe_push (insn);
3678 }
3679 else
3680 {
3681 record_insn_defs (insn);
3682
3683 // If we've decided not to allocate the current region, and if
3684 // all relevant registers are now dead, consider splitting the
3685 // block into two regions at this point.
3686 //
3687 // This is useful when a sequence of vector code ends in a
3688 // vector-to-scalar-integer operation. We wouldn't normally
3689 // want to allocate the scalar integer, since IRA is much better
3690 // at handling cross-file moves. But if nothing of relevance is
3691 // live between the death of the final vector and the birth of the
3692 // scalar integer, we can try to split the region at that point.
3693 // We'd then allocate the vector input and leave the real RA
3694 // to handle the scalar output.
3695 if (is_isolated
3696 && !m_allocation_successful
3697 && could_split_region_here ()
3698 && !defines_multi_def_pseudo (insn))
3699 {
3700 // Mark that we've deliberately chosen to leave the output
3701 // pseudos to the real RA.
3702 df_ref ref;
3703 FOR_EACH_INSN_DEF (ref, insn)
3704 {
3705 unsigned int regno = DF_REF_REGNO (ref);
3706 if (!HARD_REGISTER_NUM_P (regno))
3707 {
3708 if (dump_file && (dump_flags & TDF_DETAILS))
3709 fprintf (dump_file, "Ignoring register r%d\n", regno);
3710 m_pseudo_regs[regno].flags |= IGNORE_REG;
3711 bitmap_clear_bit (m_fpr_pseudos, regno);
3712 }
3713 }
3714
3715 if (dump_file && (dump_flags & TDF_DETAILS))
3716 fprintf (dump_file, "\nStarting new region at insn %d\n",
3717 INSN_UID (insn));
3718
3719 // Start a new region and replay the definitions. The replay
3720 // is needed if the instruction sets a hard FP register.
3721 start_new_region ();
3722 record_insn_defs (insn);
3723 start_insn = insn;
3724 }
3725
3726 if (auto *call_insn = dyn_cast<rtx_call_insn *> (insn))
3727 record_insn_call (call_insn);
3728 record_insn_uses (insn);
3729 rtx pat = PATTERN (insn);
3730 if (is_move_set (pat))
3731 record_copy (SET_DEST (pat), SET_SRC (pat), true);
3732 else
3733 {
3734 extract_insn (insn);
3735 record_constraints (insn);
3736 }
3737 }
3738
3739 // See whether we have a complete region, with no remaining live
3740 // allocnos.
3741 if (is_isolated && could_split_region_here ())
3742 {
3743 rtx_insn *prev_insn = PREV_INSN (insn);
3744 if (m_allocation_successful)
3745 {
3746 m_insn_ranges.safe_push ({ start_insn, prev_insn });
3747 process_region ();
3748 }
3749 start_new_region ();
3750 is_first = true;
3751 start_insn = prev_insn;
3752 }
3753 }
3754 m_insn_ranges.safe_push ({ start_insn, BB_HEAD (bb) });
3755
3756 record_artificial_refs (DF_REF_AT_TOP);
3757
3758 // Process live-in FPRs.
3759 bitmap live_in = df_get_live_in (bb);
3760 for (unsigned int regno = V0_REGNUM; regno <= V31_REGNUM; ++regno)
3761 if (bitmap_bit_p (live_in, regno)
3762 && (m_live_fprs & (1U << (regno - V0_REGNUM))))
3763 record_fpr_def (regno);
3764
3765 // Process live-in allocnos.
3766 EXECUTE_IF_AND_IN_BITMAP (live_in, m_fpr_pseudos,
3767 FIRST_PSEUDO_REGISTER, regno, bi)
3768 {
3769 auto range = get_allocno_subgroup (regno_reg_rtx[regno]);
3770 for (auto &allocno : range.allocnos ())
3771 if (bitmap_bit_p (m_live_allocnos, allocno.id))
3772 record_allocno_def (&allocno);
3773 }
3774
3775 m_current_point += 1;
3776
3777 bitmap_clear (m_live_allocnos);
3778 m_live_fprs = 0;
3779 }
3780
3781 // Divide the function into regions, such that there no edges into or out
3782 // of the region have live "FPR pseudos".
3783 void
3784 early_ra::process_blocks ()
3785 {
3786 auto_sbitmap visited (last_basic_block_for_fn (m_fn));
3787 auto_sbitmap fpr_pseudos_live_out (last_basic_block_for_fn (m_fn));
3788 auto_sbitmap fpr_pseudos_live_in (last_basic_block_for_fn (m_fn));
3789
3790 bitmap_clear (visited);
3791 bitmap_clear (fpr_pseudos_live_out);
3792 bitmap_clear (fpr_pseudos_live_in);
3793
3794 // Record which blocks have live FPR pseudos on entry and exit.
3795 basic_block bb;
3796 FOR_EACH_BB_FN (bb, m_fn)
3797 {
3798 if (bitmap_intersect_p (df_get_live_out (bb), m_fpr_pseudos))
3799 bitmap_set_bit (fpr_pseudos_live_out, bb->index);
3800 if (bitmap_intersect_p (df_get_live_in (bb), m_fpr_pseudos))
3801 bitmap_set_bit (fpr_pseudos_live_in, bb->index);
3802 }
3803
3804 // This is incremented by 1 at the start of each region.
3805 m_current_region = 0;
3806 memset (m_fpr_recency, 0, sizeof (m_fpr_recency));
3807
3808 struct stack_node { edge_iterator ei; basic_block bb; };
3809
3810 auto_vec<stack_node, 32> stack;
3811 auto_vec<basic_block, 32> region;
3812
3813 // Go through the function in reverse postorder and process the region
3814 // containing each block.
3815 unsigned int n_blocks = df_get_n_blocks (DF_FORWARD);
3816 int *order = df_get_postorder (DF_FORWARD);
3817 for (unsigned int bbi = 0; bbi < n_blocks; ++bbi)
3818 {
3819 basic_block bb = BASIC_BLOCK_FOR_FN (m_fn, order[bbi]);
3820 if (bb->index < NUM_FIXED_BLOCKS)
3821 continue;
3822
3823 if (!bitmap_set_bit (visited, bb->index))
3824 continue;
3825
3826 // Process forward edges before backward edges (so push backward
3827 // edges first). Build the region in an approximation of reverse
3828 // program order.
3829 if (bitmap_bit_p (fpr_pseudos_live_in, bb->index))
3830 stack.quick_push ({ ei_start (bb->preds), nullptr });
3831 if (bitmap_bit_p (fpr_pseudos_live_out, bb->index))
3832 stack.quick_push ({ ei_start (bb->succs), bb });
3833 else
3834 region.safe_push (bb);
3835 while (!stack.is_empty ())
3836 {
3837 auto &node = stack.last ();
3838 if (ei_end_p (node.ei))
3839 {
3840 if (node.bb)
3841 region.safe_push (node.bb);
3842 stack.pop ();
3843 continue;
3844 }
3845 edge e = ei_edge (node.ei);
3846 if (node.bb)
3847 {
3848 // A forward edge from a node that has not yet been added
3849 // to region.
3850 if (bitmap_bit_p (fpr_pseudos_live_in, e->dest->index)
3851 && bitmap_set_bit (visited, e->dest->index))
3852 {
3853 stack.safe_push ({ ei_start (e->dest->preds), nullptr });
3854 if (bitmap_bit_p (fpr_pseudos_live_out, e->dest->index))
3855 stack.safe_push ({ ei_start (e->dest->succs), e->dest });
3856 else
3857 region.safe_push (e->dest);
3858 }
3859 else
3860 ei_next (&node.ei);
3861 }
3862 else
3863 {
3864 // A backward edge from a node that has already been added
3865 // to the region.
3866 if (bitmap_bit_p (fpr_pseudos_live_out, e->src->index)
3867 && bitmap_set_bit (visited, e->src->index))
3868 {
3869 if (bitmap_bit_p (fpr_pseudos_live_in, e->src->index))
3870 stack.safe_push ({ ei_start (e->src->preds), nullptr });
3871 stack.safe_push ({ ei_start (e->src->succs), e->src });
3872 }
3873 else
3874 ei_next (&node.ei);
3875 }
3876 }
3877
3878 m_current_point = 2;
3879 start_new_region ();
3880
3881 if (region.is_empty ())
3882 process_block (bb, true);
3883 else
3884 {
3885 if (dump_file && (dump_flags & TDF_DETAILS))
3886 {
3887 fprintf (dump_file, "\nRegion (from %d):", bb->index);
3888 for (unsigned int j = 0; j < region.length (); ++j)
3889 fprintf (dump_file, " %d", region[j]->index);
3890 fprintf (dump_file, "\n");
3891 }
3892 for (unsigned int j = 0; j < region.length (); ++j)
3893 {
3894 basic_block bb = region[j];
3895 bool is_isolated
3896 = ((j == 0 && !bitmap_bit_p (fpr_pseudos_live_out, bb->index))
3897 || (j == region.length () - 1
3898 && !bitmap_bit_p (fpr_pseudos_live_in, bb->index)));
3899 process_block (bb, is_isolated);
3900 }
3901 }
3902 region.truncate (0);
3903
3904 if (!m_allocnos.is_empty () && m_allocation_successful)
3905 process_region ();
3906 }
3907 }
3908
3909 // Run the pass on the current function.
3910 void
3911 early_ra::execute ()
3912 {
3913 df_analyze ();
3914
3915 preprocess_insns ();
3916 propagate_pseudo_reg_info ();
3917 choose_fpr_pseudos ();
3918 if (bitmap_empty_p (m_fpr_pseudos))
3919 return;
3920
3921 if (dump_file && (dump_flags & TDF_DETAILS))
3922 dump_pseudo_regs ();
3923
3924 process_blocks ();
3925 df_verify ();
3926 }
3927
3928 class pass_early_ra : public rtl_opt_pass
3929 {
3930 public:
3931 pass_early_ra (gcc::context *ctxt)
3932 : rtl_opt_pass (pass_data_early_ra, ctxt)
3933 {}
3934
3935 // opt_pass methods:
3936 virtual bool gate (function *);
3937 virtual unsigned int execute (function *);
3938 };
3939
3940 bool
3941 pass_early_ra::gate (function *)
3942 {
3943 // Require a vector ISA to be enabled.
3944 if (!TARGET_SIMD && !TARGET_SVE)
3945 return false;
3946
3947 if (aarch64_early_ra == AARCH64_EARLY_RA_NONE)
3948 return false;
3949
3950 if (aarch64_early_ra == AARCH64_EARLY_RA_STRIDED
3951 && !TARGET_STREAMING_SME2)
3952 return false;
3953
3954 return true;
3955 }
3956
3957 unsigned int
3958 pass_early_ra::execute (function *fn)
3959 {
3960 early_ra (fn).execute ();
3961 return 0;
3962 }
3963
3964 } // end namespace
3965
3966 // Create a new instance of the pass.
3967 rtl_opt_pass *
3968 make_pass_aarch64_early_ra (gcc::context *ctxt)
3969 {
3970 return new pass_early_ra (ctxt);
3971 }