1 /* Rematerialize pseudos values.
2 Copyright (C) 2014-2019 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This code objective is to rematerialize spilled pseudo values. To
22 do this we calculate available insn candidates. The candidate is
23 available at some point if there is dominated set of insns with the
24 same pattern, the insn inputs are not dying or modified on any path
25 from the set, the outputs are not modified.
27 The insns containing memory or spilled pseudos (except for the
28 rematerialized pseudo) are not considered as such insns are not
29 profitable in comparison with regular loads of spilled pseudo
30 values. That simplifies the implementation as we don't need to
31 deal with memory aliasing.
33 To speed up available candidate calculation, we calculate partially
34 available candidates first and use them for initialization of the
35 availability. That is because (partial) availability sets are
38 The rematerialization sub-pass could be improved further in the
41 o We could make longer live ranges of inputs in the
42 rematerialization candidates if their hard registers are not used
43 for other purposes. This could be complicated if we need to
44 update BB live info information as LRA does not use
45 DF-infrastructure for compile-time reasons. This problem could
46 be overcome if constrain making live ranges longer only in BB/EBB
48 o We could use cost-based decision to choose rematerialization insn
49 (currently all insns without memory is can be used).
50 o We could use other free hard regs for unused output pseudos in
51 rematerialization candidates although such cases probably will
57 #include "coretypes.h"
61 #include "insn-config.h"
69 /* Number of candidates for rematerialization. */
70 static unsigned int cands_num
;
72 /* The following is used for representation of call_used_reg_set in
73 form array whose elements are hard register numbers with nonzero bit
74 in CALL_USED_REG_SET. */
75 static int call_used_regs_arr_len
;
76 static int call_used_regs_arr
[FIRST_PSEUDO_REGISTER
];
78 /* Bitmap used for different calculations. */
79 static bitmap_head temp_bitmap
;
81 /* Registers accessed via subreg_p. */
82 static bitmap_head subreg_regs
;
84 typedef struct cand
*cand_t
;
85 typedef const struct cand
*const_cand_t
;
87 /* Insn candidates for rematerialization. The candidate insn should
88 have the following properies:
89 o no any memory (as access to memory is non-profitable)
90 o no INOUT regs (it means no non-paradoxical subreg of output reg)
91 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
92 o all other pseudos are with assigned hard regs. */
95 /* Index of the candidates in all_cands. */
97 /* Insn pseudo regno for rematerialization. */
99 /* The candidate insn. */
101 /* Non-negative if a reload pseudo is in the insn instead of the
102 pseudo for rematerialization. */
104 /* Number of the operand containing the regno or its reload
107 /* Next candidate for the same regno. */
108 cand_t next_regno_cand
;
111 /* Vector containing all candidates. */
112 static vec
<cand_t
> all_cands
;
113 /* Map: insn -> candidate representing it. It is null if the insn cannot
114 be used for rematerialization. */
115 static cand_t
*insn_to_cand
;
116 /* A secondary map, for candidates that involve two insns, where the
117 second one makes the equivalence. The candidate must not be used
118 before seeing this activation insn. */
119 static cand_t
*insn_to_cand_activation
;
121 /* Map regno -> candidates can be used for the regno
122 rematerialization. */
123 static cand_t
*regno_cands
;
125 /* Data about basic blocks used for the rematerialization
130 /* Basic block about which the below data are. */
132 /* Registers changed in the basic block: */
133 bitmap_head changed_regs
;
134 /* Registers becoming dead in the BB. */
135 bitmap_head dead_regs
;
136 /* Cands present in the BB whose in/out regs are not changed after
137 the cands occurence and are not dead (except the reload
139 bitmap_head gen_cands
;
140 bitmap_head livein_cands
; /* cands whose inputs live at the BB start. */
141 bitmap_head pavin_cands
; /* cands partially available at BB entry. */
142 bitmap_head pavout_cands
; /* cands partially available at BB exit. */
143 bitmap_head avin_cands
; /* cands available at the entry of the BB. */
144 bitmap_head avout_cands
; /* cands available at the exit of the BB. */
147 /* Array for all BB data. Indexed by the corresponding BB index. */
148 typedef class remat_bb_data
*remat_bb_data_t
;
150 /* Basic blocks for data flow problems -- all bocks except the special
152 static bitmap_head all_blocks
;
154 /* All basic block data are referred through the following array. */
155 static remat_bb_data_t remat_bb_data
;
157 /* Two small functions for access to the bb data. */
158 static inline remat_bb_data_t
159 get_remat_bb_data (basic_block bb
)
161 return &remat_bb_data
[(bb
)->index
];
164 static inline remat_bb_data_t
165 get_remat_bb_data_by_index (int index
)
167 return &remat_bb_data
[index
];
172 /* Hash table for the candidates. Different insns (e.g. structurally
173 the same insns or even insns with different unused output regs) can
174 be represented by the same candidate in the table. */
175 static htab_t cand_table
;
177 /* Hash function for candidate CAND. */
179 cand_hash (const void *cand
)
181 const_cand_t c
= (const_cand_t
) cand
;
182 lra_insn_recog_data_t id
= lra_get_insn_recog_data (c
->insn
);
183 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
184 int nops
= static_id
->n_operands
;
187 for (int i
= 0; i
< nops
; i
++)
189 hash
= iterative_hash_object (c
->regno
, hash
);
190 else if (static_id
->operand
[i
].type
== OP_IN
)
191 hash
= iterative_hash_object (*id
->operand_loc
[i
], hash
);
195 /* Equal function for candidates CAND1 and CAND2. They are equal if
196 the corresponding candidate insns have the same code, the same
197 regno for rematerialization, the same input operands. */
199 cand_eq_p (const void *cand1
, const void *cand2
)
201 const_cand_t c1
= (const_cand_t
) cand1
;
202 const_cand_t c2
= (const_cand_t
) cand2
;
203 lra_insn_recog_data_t id1
= lra_get_insn_recog_data (c1
->insn
);
204 lra_insn_recog_data_t id2
= lra_get_insn_recog_data (c2
->insn
);
205 struct lra_static_insn_data
*static_id1
= id1
->insn_static_data
;
206 int nops
= static_id1
->n_operands
;
208 if (c1
->regno
!= c2
->regno
209 || INSN_CODE (c1
->insn
) < 0
210 || INSN_CODE (c1
->insn
) != INSN_CODE (c2
->insn
))
212 gcc_assert (c1
->nop
== c2
->nop
);
213 for (int i
= 0; i
< nops
; i
++)
214 if (i
!= c1
->nop
&& static_id1
->operand
[i
].type
== OP_IN
215 && *id1
->operand_loc
[i
] != *id2
->operand_loc
[i
])
220 /* Insert candidate CAND into the table if it is not there yet.
221 Return candidate which is in the table. */
223 insert_cand (cand_t cand
)
227 entry_ptr
= htab_find_slot (cand_table
, cand
, INSERT
);
228 if (*entry_ptr
== NULL
)
229 *entry_ptr
= (void *) cand
;
230 return (cand_t
) *entry_ptr
;
233 /* Free candidate CAND memory. */
235 free_cand (void *cand
)
240 /* Initiate the candidate table. */
242 initiate_cand_table (void)
244 cand_table
= htab_create (8000, cand_hash
, cand_eq_p
,
245 (htab_del
) free_cand
);
248 /* Finish the candidate table. */
250 finish_cand_table (void)
252 htab_delete (cand_table
);
257 /* Return true if X contains memory or some UNSPEC. We cannot just
258 check insn operands as memory or unspec might be not an operand
259 itself but contain an operand. Insn with memory access is not
260 profitable for rematerialization. Rematerialization of UNSPEC
261 might result in wrong code generation as the UNPEC effect is
262 unknown (e.g. generating a label). */
264 bad_for_rematerialization_p (rtx x
)
270 if (MEM_P (x
) || GET_CODE (x
) == UNSPEC
|| GET_CODE (x
) == UNSPEC_VOLATILE
)
273 fmt
= GET_RTX_FORMAT (code
);
274 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
278 if (bad_for_rematerialization_p (XEXP (x
, i
)))
281 else if (fmt
[i
] == 'E')
283 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
284 if (bad_for_rematerialization_p (XVECEXP (x
, i
, j
)))
291 /* If INSN cannot be used for rematerialization, return negative
292 value. If INSN can be considered as a candidate for
293 rematerialization, return value which is the operand number of the
294 pseudo for which the insn can be used for rematerialization. Here
295 we consider the insns without any memory, spilled pseudo (except
296 for the rematerialization pseudo), or dying or unused regs. */
298 operand_to_remat (rtx_insn
*insn
)
300 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
301 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
302 struct lra_insn_reg
*reg
, *found_reg
= NULL
;
304 /* Don't rematerialize insns which can change PC. */
305 if (JUMP_P (insn
) || CALL_P (insn
))
307 /* First find a pseudo which can be rematerialized. */
308 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
310 /* True FRAME_POINTER_NEEDED might be because we cannot follow
311 changing sp offsets, e.g. alloca is used. If the insn contains
312 stack pointer in such case, we cannot rematerialize it as we
313 cannot know sp offset at a rematerialization place. */
314 if (reg
->regno
== STACK_POINTER_REGNUM
&& frame_pointer_needed
)
316 else if (reg
->type
== OP_OUT
&& ! reg
->subreg_p
317 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
319 /* We permits only one spilled reg. */
320 if (found_reg
!= NULL
)
324 /* IRA calculates conflicts separately for subregs of two words
325 pseudo. Even if the pseudo lives, e.g. one its subreg can be
326 used lately, another subreg hard register can be already used
327 for something else. In such case, it is not safe to
328 rematerialize the insn. */
329 if (reg
->regno
>= FIRST_PSEUDO_REGISTER
330 && bitmap_bit_p (&subreg_regs
, reg
->regno
))
333 /* Don't allow hard registers to be rematerialized. */
334 if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
337 if (found_reg
== NULL
)
339 if (found_reg
->regno
< FIRST_PSEUDO_REGISTER
)
341 if (bad_for_rematerialization_p (PATTERN (insn
)))
343 /* Check the other regs are not spilled. */
344 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
345 if (found_reg
== reg
)
347 else if (reg
->type
== OP_INOUT
)
349 else if (reg
->regno
>= FIRST_PSEUDO_REGISTER
350 && reg_renumber
[reg
->regno
] < 0)
351 /* Another spilled reg. */
353 else if (reg
->type
== OP_IN
)
355 if (find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
356 /* We don't want to make live ranges longer. */
358 /* Check that there is no output reg as the input one. */
359 for (struct lra_insn_reg
*reg2
= id
->regs
;
362 if (reg2
->type
== OP_OUT
&& reg
->regno
== reg2
->regno
)
364 if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
365 for (struct lra_insn_reg
*reg2
= static_id
->hard_regs
;
368 if (reg2
->type
== OP_OUT
369 && reg
->regno
<= reg2
->regno
371 < (int) end_hard_regno (reg
->biggest_mode
, reg
->regno
)))
374 /* Check hard coded insn registers. */
375 for (struct lra_insn_reg
*reg
= static_id
->hard_regs
;
378 if (reg
->type
== OP_INOUT
)
380 else if (reg
->type
== OP_IN
)
382 /* Check that there is no output hard reg as the input
384 for (struct lra_insn_reg
*reg2
= static_id
->hard_regs
;
387 if (reg2
->type
== OP_OUT
&& reg
->regno
== reg2
->regno
)
390 /* Find the rematerialization operand. */
391 int nop
= static_id
->n_operands
;
392 for (int i
= 0; i
< nop
; i
++)
393 if (REG_P (*id
->operand_loc
[i
])
394 && (int) REGNO (*id
->operand_loc
[i
]) == found_reg
->regno
)
399 /* Create candidate for INSN with rematerialization operand NOP and
400 REGNO. Insert the candidate into the table and set up the
401 corresponding INSN_TO_CAND element. */
403 create_cand (rtx_insn
*insn
, int nop
, int regno
, rtx_insn
*activation
= NULL
)
405 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
406 rtx reg
= *id
->operand_loc
[nop
];
407 gcc_assert (REG_P (reg
));
408 int op_regno
= REGNO (reg
);
409 gcc_assert (op_regno
>= FIRST_PSEUDO_REGISTER
);
410 cand_t cand
= XNEW (struct cand
);
414 cand
->reload_regno
= op_regno
== regno
? -1 : op_regno
;
415 gcc_assert (cand
->regno
>= 0);
416 cand_t cand_in_table
= insert_cand (cand
);
417 insn_to_cand
[INSN_UID (insn
)] = cand_in_table
;
418 if (cand
!= cand_in_table
)
423 cand
->index
= all_cands
.length ();
424 all_cands
.safe_push (cand
);
425 cand
->next_regno_cand
= regno_cands
[cand
->regno
];
426 regno_cands
[cand
->regno
] = cand
;
429 insn_to_cand_activation
[INSN_UID (activation
)] = cand_in_table
;
432 /* Create rematerialization candidates (inserting them into the
438 struct potential_cand
443 struct potential_cand
*regno_potential_cand
;
445 /* Create candidates. */
446 regno_potential_cand
= XCNEWVEC (struct potential_cand
, max_reg_num ());
447 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
448 if (NONDEBUG_INSN_P (insn
))
450 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
452 rtx set
= single_set (insn
);
455 /* See if this is an output reload for a previous insn. */
457 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
459 rtx dstreg
= SET_DEST (set
);
460 int src_regno
= REGNO (SET_SRC (set
));
461 int dst_regno
= REGNO (dstreg
);
462 rtx_insn
*insn2
= regno_potential_cand
[src_regno
].insn
;
465 && dst_regno
>= FIRST_PSEUDO_REGISTER
466 && reg_renumber
[dst_regno
] < 0
467 && BLOCK_FOR_INSN (insn2
) == BLOCK_FOR_INSN (insn
))
469 create_cand (insn2
, regno_potential_cand
[src_regno
].nop
,
475 nop
= operand_to_remat (insn
);
478 gcc_assert (REG_P (*id
->operand_loc
[nop
]));
479 int regno
= REGNO (*id
->operand_loc
[nop
]);
480 gcc_assert (regno
>= FIRST_PSEUDO_REGISTER
);
481 /* If we're setting an unrenumbered pseudo, make a candidate immediately.
482 If it's an output reload register, save it for later; the code above
483 looks for output reload insns later on. */
484 if (reg_renumber
[regno
] < 0)
485 create_cand (insn
, nop
, regno
);
486 else if (regno
>= lra_constraint_new_regno_start
)
488 regno_potential_cand
[regno
].insn
= insn
;
489 regno_potential_cand
[regno
].nop
= nop
;
495 for (struct lra_insn_reg
*reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
496 if (reg
->type
!= OP_IN
&& reg
->regno
!= keep_regno
497 && reg
->regno
>= FIRST_PSEUDO_REGISTER
)
498 regno_potential_cand
[reg
->regno
].insn
= NULL
;
500 cands_num
= all_cands
.length ();
501 free (regno_potential_cand
);
506 /* Create and initialize BB data. */
508 create_remat_bb_data (void)
511 remat_bb_data_t bb_info
;
513 remat_bb_data
= XNEWVEC (class remat_bb_data
,
514 last_basic_block_for_fn (cfun
));
515 FOR_ALL_BB_FN (bb
, cfun
)
517 gcc_checking_assert (bb
->index
>= 0
518 && bb
->index
< last_basic_block_for_fn (cfun
));
519 bb_info
= get_remat_bb_data (bb
);
521 bitmap_initialize (&bb_info
->changed_regs
, ®_obstack
);
522 bitmap_initialize (&bb_info
->dead_regs
, ®_obstack
);
523 bitmap_initialize (&bb_info
->gen_cands
, ®_obstack
);
524 bitmap_initialize (&bb_info
->livein_cands
, ®_obstack
);
525 bitmap_initialize (&bb_info
->pavin_cands
, ®_obstack
);
526 bitmap_initialize (&bb_info
->pavout_cands
, ®_obstack
);
527 bitmap_initialize (&bb_info
->avin_cands
, ®_obstack
);
528 bitmap_initialize (&bb_info
->avout_cands
, ®_obstack
);
532 /* Dump all candidates to DUMP_FILE. */
534 dump_cands (FILE *dump_file
)
539 fprintf (dump_file
, "\nCands:\n");
540 for (i
= 0; i
< (int) cands_num
; i
++)
543 fprintf (dump_file
, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
544 i
, cand
->nop
, cand
->regno
, cand
->reload_regno
);
545 print_inline_rtx (dump_file
, cand
->insn
, 6);
546 fprintf (dump_file
, "\n");
550 /* Dump all candidates and BB data. */
552 dump_candidates_and_remat_bb_data (void)
556 if (lra_dump_file
== NULL
)
558 dump_cands (lra_dump_file
);
559 FOR_EACH_BB_FN (bb
, cfun
)
561 fprintf (lra_dump_file
, "\nBB %d:\n", bb
->index
);
563 fprintf (lra_dump_file
, " register live in:");
564 dump_regset (df_get_live_in (bb
), lra_dump_file
);
565 putc ('\n', lra_dump_file
);
567 fprintf (lra_dump_file
, " register live out:");
568 dump_regset (df_get_live_out (bb
), lra_dump_file
);
569 putc ('\n', lra_dump_file
);
570 /* Changed/dead regs: */
571 fprintf (lra_dump_file
, " changed regs:");
572 dump_regset (&get_remat_bb_data (bb
)->changed_regs
, lra_dump_file
);
573 putc ('\n', lra_dump_file
);
574 fprintf (lra_dump_file
, " dead regs:");
575 dump_regset (&get_remat_bb_data (bb
)->dead_regs
, lra_dump_file
);
576 putc ('\n', lra_dump_file
);
577 lra_dump_bitmap_with_title ("cands generated in BB",
578 &get_remat_bb_data (bb
)->gen_cands
, bb
->index
);
579 lra_dump_bitmap_with_title ("livein cands in BB",
580 &get_remat_bb_data (bb
)->livein_cands
, bb
->index
);
581 lra_dump_bitmap_with_title ("pavin cands in BB",
582 &get_remat_bb_data (bb
)->pavin_cands
, bb
->index
);
583 lra_dump_bitmap_with_title ("pavout cands in BB",
584 &get_remat_bb_data (bb
)->pavout_cands
, bb
->index
);
585 lra_dump_bitmap_with_title ("avin cands in BB",
586 &get_remat_bb_data (bb
)->avin_cands
, bb
->index
);
587 lra_dump_bitmap_with_title ("avout cands in BB",
588 &get_remat_bb_data (bb
)->avout_cands
, bb
->index
);
590 fprintf (lra_dump_file
, "subreg regs:");
591 dump_regset (&subreg_regs
, lra_dump_file
);
592 putc ('\n', lra_dump_file
);
595 /* Free all BB data. */
597 finish_remat_bb_data (void)
601 FOR_EACH_BB_FN (bb
, cfun
)
603 bitmap_clear (&get_remat_bb_data (bb
)->avout_cands
);
604 bitmap_clear (&get_remat_bb_data (bb
)->avin_cands
);
605 bitmap_clear (&get_remat_bb_data (bb
)->pavout_cands
);
606 bitmap_clear (&get_remat_bb_data (bb
)->pavin_cands
);
607 bitmap_clear (&get_remat_bb_data (bb
)->livein_cands
);
608 bitmap_clear (&get_remat_bb_data (bb
)->gen_cands
);
609 bitmap_clear (&get_remat_bb_data (bb
)->dead_regs
);
610 bitmap_clear (&get_remat_bb_data (bb
)->changed_regs
);
612 free (remat_bb_data
);
617 /* Update changed_regs, dead_regs, subreg_regs of BB from INSN. */
619 set_bb_regs (basic_block bb
, rtx_insn
*insn
)
621 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
622 remat_bb_data_t bb_info
= get_remat_bb_data (bb
);
623 struct lra_insn_reg
*reg
;
625 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
627 unsigned regno
= reg
->regno
;
628 if (reg
->type
!= OP_IN
)
629 bitmap_set_bit (&bb_info
->changed_regs
, regno
);
630 else if (find_regno_note (insn
, REG_DEAD
, regno
) != NULL
)
631 bitmap_set_bit (&bb_info
->dead_regs
, regno
);
632 if (regno
>= FIRST_PSEUDO_REGISTER
&& reg
->subreg_p
)
633 bitmap_set_bit (&subreg_regs
, regno
);
636 for (int i
= 0; i
< call_used_regs_arr_len
; i
++)
637 bitmap_set_bit (&get_remat_bb_data (bb
)->dead_regs
,
638 call_used_regs_arr
[i
]);
641 /* Calculate changed_regs and dead_regs for each BB. */
643 calculate_local_reg_remat_bb_data (void)
648 FOR_EACH_BB_FN (bb
, cfun
)
649 FOR_BB_INSNS (bb
, insn
)
650 if (NONDEBUG_INSN_P (insn
))
651 set_bb_regs (bb
, insn
);
656 /* Return true if REG overlaps an input operand of INSN. */
658 reg_overlap_for_remat_p (lra_insn_reg
*reg
, rtx_insn
*insn
)
661 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
662 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
663 unsigned regno
= reg
->regno
;
666 if (regno
>= FIRST_PSEUDO_REGISTER
&& reg_renumber
[regno
] >= 0)
667 regno
= reg_renumber
[regno
];
668 if (regno
>= FIRST_PSEUDO_REGISTER
)
671 nregs
= hard_regno_nregs (regno
, reg
->biggest_mode
);
673 struct lra_insn_reg
*reg2
;
675 for (iter
= 0; iter
< 2; iter
++)
676 for (reg2
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
680 if (reg2
->type
!= OP_IN
)
682 unsigned regno2
= reg2
->regno
;
685 if (regno2
>= FIRST_PSEUDO_REGISTER
&& reg_renumber
[regno2
] >= 0)
686 regno2
= reg_renumber
[regno2
];
687 if (regno2
>= FIRST_PSEUDO_REGISTER
)
690 nregs2
= hard_regno_nregs (regno2
, reg
->biggest_mode
);
692 if ((regno2
+ nregs2
- 1 >= regno
&& regno2
< regno
+ nregs
)
693 || (regno
+ nregs
- 1 >= regno2
&& regno
< regno2
+ nregs2
))
699 /* Return true if a call used register is an input operand of INSN. */
701 call_used_input_regno_present_p (rtx_insn
*insn
)
704 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
705 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
706 struct lra_insn_reg
*reg
;
708 for (iter
= 0; iter
< 2; iter
++)
709 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
712 if (reg
->type
== OP_IN
&& reg
->regno
< FIRST_PSEUDO_REGISTER
713 && TEST_HARD_REG_BIT (call_used_reg_set
, reg
->regno
))
718 /* Calculate livein_cands for each BB. */
720 calculate_livein_cands (void)
724 FOR_EACH_BB_FN (bb
, cfun
)
726 bitmap livein_regs
= df_get_live_in (bb
);
727 bitmap livein_cands
= &get_remat_bb_data (bb
)->livein_cands
;
728 for (unsigned int i
= 0; i
< cands_num
; i
++)
730 cand_t cand
= all_cands
[i
];
731 lra_insn_recog_data_t id
= lra_get_insn_recog_data (cand
->insn
);
732 struct lra_insn_reg
*reg
;
734 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
735 if (reg
->type
== OP_IN
&& ! bitmap_bit_p (livein_regs
, reg
->regno
))
738 bitmap_set_bit (livein_cands
, i
);
743 /* Calculate gen_cands for each BB. */
745 calculate_gen_cands (void)
751 FOR_EACH_BB_FN (bb
, cfun
)
753 gen_cands
= &get_remat_bb_data (bb
)->gen_cands
;
754 auto_bitmap
gen_insns (®_obstack
);
755 FOR_BB_INSNS (bb
, insn
)
758 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
759 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
760 struct lra_insn_reg
*reg
;
766 int src_regno
= -1, dst_regno
= -1;
768 if ((set
= single_set (insn
)) != NULL
769 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
771 src_regno
= REGNO (SET_SRC (set
));
772 dst_regno
= REGNO (SET_DEST (set
));
775 /* Update gen_cands: */
776 bitmap_clear (&temp_bitmap
);
777 for (iter
= 0; iter
< 2; iter
++)
778 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
781 if (reg
->type
!= OP_IN
782 || find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
783 EXECUTE_IF_SET_IN_BITMAP (gen_insns
, 0, uid
, bi
)
785 rtx_insn
*insn2
= lra_insn_recog_data
[uid
]->insn
;
787 cand
= insn_to_cand
[INSN_UID (insn2
)];
788 gcc_assert (cand
!= NULL
);
789 /* Ignore the reload insn. */
790 if (src_regno
== cand
->reload_regno
791 && dst_regno
== cand
->regno
)
793 if (cand
->regno
== reg
->regno
794 || reg_overlap_for_remat_p (reg
, insn2
))
796 bitmap_clear_bit (gen_cands
, cand
->index
);
797 bitmap_set_bit (&temp_bitmap
, uid
);
802 EXECUTE_IF_SET_IN_BITMAP (gen_insns
, 0, uid
, bi
)
804 rtx_insn
*insn2
= lra_insn_recog_data
[uid
]->insn
;
806 cand
= insn_to_cand
[INSN_UID (insn2
)];
807 gcc_assert (cand
!= NULL
);
808 if (call_used_input_regno_present_p (insn2
))
810 bitmap_clear_bit (gen_cands
, cand
->index
);
811 bitmap_set_bit (&temp_bitmap
, uid
);
814 bitmap_and_compl_into (gen_insns
, &temp_bitmap
);
816 cand
= insn_to_cand
[INSN_UID (insn
)];
819 bitmap_set_bit (gen_cands
, cand
->index
);
820 bitmap_set_bit (gen_insns
, INSN_UID (insn
));
828 /* The common transfer function used by the DF equation solver to
829 propagate (partial) availability info BB_IN to BB_OUT through block
830 with BB_INDEX according to the following equation:
832 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
835 cand_trans_fun (int bb_index
, bitmap bb_in
, bitmap bb_out
)
837 remat_bb_data_t bb_info
;
838 bitmap bb_livein
, bb_changed_regs
, bb_dead_regs
;
842 bb_info
= get_remat_bb_data_by_index (bb_index
);
843 bb_livein
= &bb_info
->livein_cands
;
844 bb_changed_regs
= &bb_info
->changed_regs
;
845 bb_dead_regs
= &bb_info
->dead_regs
;
846 /* Calculate killed avin cands -- cands whose regs are changed or
847 becoming dead in the BB. We calculate it here as we hope that
848 repeated calculations are compensated by smaller size of BB_IN in
849 comparison with all candidates number. */
850 bitmap_clear (&temp_bitmap
);
851 EXECUTE_IF_SET_IN_BITMAP (bb_in
, 0, cid
, bi
)
853 cand_t cand
= all_cands
[cid
];
854 lra_insn_recog_data_t id
= lra_get_insn_recog_data (cand
->insn
);
855 struct lra_insn_reg
*reg
;
857 if (! bitmap_bit_p (bb_livein
, cid
))
859 bitmap_set_bit (&temp_bitmap
, cid
);
862 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
863 /* Ignore all outputs which are not the regno for
864 rematerialization. */
865 if (reg
->type
== OP_OUT
&& reg
->regno
!= cand
->regno
)
867 else if (bitmap_bit_p (bb_changed_regs
, reg
->regno
)
868 || bitmap_bit_p (bb_dead_regs
, reg
->regno
))
870 bitmap_set_bit (&temp_bitmap
, cid
);
873 /* Check regno for rematerialization. */
874 if (bitmap_bit_p (bb_changed_regs
, cand
->regno
)
875 || bitmap_bit_p (bb_dead_regs
, cand
->regno
))
876 bitmap_set_bit (&temp_bitmap
, cid
);
878 return bitmap_ior_and_compl (bb_out
,
879 &bb_info
->gen_cands
, bb_in
, &temp_bitmap
);
884 /* The transfer function used by the DF equation solver to propagate
885 partial candidate availability info through block with BB_INDEX
886 according to the following equation:
888 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
891 cand_pav_trans_fun (int bb_index
)
893 remat_bb_data_t bb_info
;
895 bb_info
= get_remat_bb_data_by_index (bb_index
);
896 return cand_trans_fun (bb_index
, &bb_info
->pavin_cands
,
897 &bb_info
->pavout_cands
);
900 /* The confluence function used by the DF equation solver to set up
901 cand_pav info for a block BB without predecessor. */
903 cand_pav_con_fun_0 (basic_block bb
)
905 bitmap_clear (&get_remat_bb_data (bb
)->pavin_cands
);
908 /* The confluence function used by the DF equation solver to propagate
909 partial candidate availability info from predecessor to successor
910 on edge E (pred->bb) according to the following equation:
912 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
915 cand_pav_con_fun_n (edge e
)
917 basic_block pred
= e
->src
;
918 basic_block bb
= e
->dest
;
919 remat_bb_data_t bb_info
;
920 bitmap bb_pavin
, pred_pavout
;
922 bb_info
= get_remat_bb_data (bb
);
923 bb_pavin
= &bb_info
->pavin_cands
;
924 pred_pavout
= &get_remat_bb_data (pred
)->pavout_cands
;
925 return bitmap_ior_into (bb_pavin
, pred_pavout
);
930 /* The transfer function used by the DF equation solver to propagate
931 candidate availability info through block with BB_INDEX according
932 to the following equation:
934 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
937 cand_av_trans_fun (int bb_index
)
939 remat_bb_data_t bb_info
;
941 bb_info
= get_remat_bb_data_by_index (bb_index
);
942 return cand_trans_fun (bb_index
, &bb_info
->avin_cands
,
943 &bb_info
->avout_cands
);
946 /* The confluence function used by the DF equation solver to set up
947 cand_av info for a block BB without predecessor. */
949 cand_av_con_fun_0 (basic_block bb
)
951 bitmap_clear (&get_remat_bb_data (bb
)->avin_cands
);
954 /* The confluence function used by the DF equation solver to propagate
955 cand_av info from predecessor to successor on edge E (pred->bb)
956 according to the following equation:
958 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
961 cand_av_con_fun_n (edge e
)
963 basic_block pred
= e
->src
;
964 basic_block bb
= e
->dest
;
965 remat_bb_data_t bb_info
;
966 bitmap bb_avin
, pred_avout
;
968 bb_info
= get_remat_bb_data (bb
);
969 bb_avin
= &bb_info
->avin_cands
;
970 pred_avout
= &get_remat_bb_data (pred
)->avout_cands
;
971 return bitmap_and_into (bb_avin
, pred_avout
);
974 /* Calculate available candidates for each BB. */
976 calculate_global_remat_bb_data (void)
981 (DF_FORWARD
, NULL
, cand_pav_con_fun_0
, cand_pav_con_fun_n
,
982 cand_pav_trans_fun
, &all_blocks
,
983 df_get_postorder (DF_FORWARD
), df_get_n_blocks (DF_FORWARD
));
984 /* Initialize avin by pavin. */
985 FOR_EACH_BB_FN (bb
, cfun
)
986 bitmap_copy (&get_remat_bb_data (bb
)->avin_cands
,
987 &get_remat_bb_data (bb
)->pavin_cands
);
989 (DF_FORWARD
, NULL
, cand_av_con_fun_0
, cand_av_con_fun_n
,
990 cand_av_trans_fun
, &all_blocks
,
991 df_get_postorder (DF_FORWARD
), df_get_n_blocks (DF_FORWARD
));
996 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
998 change_sp_offset (rtx_insn
*insns
, poly_int64 sp_offset
)
1000 for (rtx_insn
*insn
= insns
; insn
!= NULL
; insn
= NEXT_INSN (insn
))
1001 eliminate_regs_in_insn (insn
, false, false, sp_offset
);
1004 /* Return start hard register of REG (can be a hard or a pseudo reg)
1005 or -1 (if it is a spilled pseudo). Return number of hard registers
1006 occupied by REG through parameter NREGS if the start hard reg is
1009 get_hard_regs (struct lra_insn_reg
*reg
, int &nregs
)
1011 int regno
= reg
->regno
;
1012 int hard_regno
= regno
< FIRST_PSEUDO_REGISTER
? regno
: reg_renumber
[regno
];
1014 if (hard_regno
>= 0)
1015 nregs
= hard_regno_nregs (hard_regno
, reg
->biggest_mode
);
1019 /* Make copy of and register scratch pseudos in rematerialized insn
1022 update_scratch_ops (rtx_insn
*remat_insn
)
1025 lra_insn_recog_data_t id
= lra_get_insn_recog_data (remat_insn
);
1026 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
1027 for (int i
= 0; i
< static_id
->n_operands
; i
++)
1029 rtx
*loc
= id
->operand_loc
[i
];
1032 int regno
= REGNO (*loc
);
1033 if (! lra_former_scratch_p (regno
))
1035 hard_regno
= reg_renumber
[regno
];
1036 *loc
= lra_create_new_reg (GET_MODE (*loc
), *loc
,
1037 lra_get_allocno_class (regno
),
1038 "scratch pseudo copy");
1039 if (hard_regno
>= 0)
1041 reg_renumber
[REGNO (*loc
)] = hard_regno
;
1043 fprintf (lra_dump_file
, " Assigning the same %d to r%d\n",
1044 REGNO (*loc
), hard_regno
);
1046 lra_register_new_scratch_op (remat_insn
, i
, id
->icode
);
1051 /* Insert rematerialization insns using the data-flow data calculated
1059 bool changed_p
= false;
1060 /* Living hard regs and hard registers of living pseudos. */
1061 HARD_REG_SET live_hard_regs
;
1064 auto_bitmap
avail_cands (®_obstack
);
1065 auto_bitmap
active_cands (®_obstack
);
1066 FOR_EACH_BB_FN (bb
, cfun
)
1068 CLEAR_HARD_REG_SET (live_hard_regs
);
1069 EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb
), 0, regno
, bi
)
1071 int hard_regno
= regno
< FIRST_PSEUDO_REGISTER
1073 : reg_renumber
[regno
];
1074 if (hard_regno
>= 0)
1075 SET_HARD_REG_BIT (live_hard_regs
, hard_regno
);
1077 bitmap_and (avail_cands
, &get_remat_bb_data (bb
)->avin_cands
,
1078 &get_remat_bb_data (bb
)->livein_cands
);
1079 /* Activating insns are always in the same block as their corresponding
1080 remat insn, so at the start of a block the two bitsets are equal. */
1081 bitmap_copy (active_cands
, avail_cands
);
1082 FOR_BB_INSNS (bb
, insn
)
1084 if (!NONDEBUG_INSN_P (insn
))
1087 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
1088 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
1089 struct lra_insn_reg
*reg
;
1095 int src_regno
= -1, dst_regno
= -1;
1097 if ((set
= single_set (insn
)) != NULL
1098 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
1100 src_regno
= REGNO (SET_SRC (set
));
1101 dst_regno
= REGNO (SET_DEST (set
));
1105 /* Check possibility of rematerialization (hard reg or
1106 unpsilled pseudo <- spilled pseudo): */
1107 if (dst_regno
>= 0 && src_regno
>= FIRST_PSEUDO_REGISTER
1108 && reg_renumber
[src_regno
] < 0
1109 && (dst_regno
< FIRST_PSEUDO_REGISTER
1110 || reg_renumber
[dst_regno
] >= 0))
1112 for (cand
= regno_cands
[src_regno
];
1114 cand
= cand
->next_regno_cand
)
1115 if (bitmap_bit_p (avail_cands
, cand
->index
)
1116 && bitmap_bit_p (active_cands
, cand
->index
))
1119 int i
, hard_regno
, nregs
;
1120 int dst_hard_regno
, dst_nregs
;
1121 rtx_insn
*remat_insn
= NULL
;
1122 poly_int64 cand_sp_offset
= 0;
1125 lra_insn_recog_data_t cand_id
1126 = lra_get_insn_recog_data (cand
->insn
);
1127 struct lra_static_insn_data
*static_cand_id
1128 = cand_id
->insn_static_data
;
1129 rtx saved_op
= *cand_id
->operand_loc
[cand
->nop
];
1131 /* Check clobbers do not kill something living. */
1132 gcc_assert (REG_P (saved_op
));
1133 int ignore_regno
= REGNO (saved_op
);
1135 dst_hard_regno
= dst_regno
< FIRST_PSEUDO_REGISTER
1136 ? dst_regno
: reg_renumber
[dst_regno
];
1137 gcc_assert (dst_hard_regno
>= 0);
1138 machine_mode mode
= GET_MODE (SET_DEST (set
));
1139 dst_nregs
= hard_regno_nregs (dst_hard_regno
, mode
);
1141 for (reg
= cand_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1142 if (reg
->type
!= OP_IN
&& reg
->regno
!= ignore_regno
)
1144 hard_regno
= get_hard_regs (reg
, nregs
);
1145 gcc_assert (hard_regno
>= 0);
1146 for (i
= 0; i
< nregs
; i
++)
1147 if (TEST_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
))
1151 /* Ensure the clobber also doesn't overlap dst_regno. */
1152 if (hard_regno
+ nregs
> dst_hard_regno
1153 && hard_regno
< dst_hard_regno
+ dst_nregs
)
1159 for (reg
= static_cand_id
->hard_regs
;
1162 if (reg
->type
!= OP_IN
)
1164 if (TEST_HARD_REG_BIT (live_hard_regs
, reg
->regno
))
1166 if (reg
->regno
>= dst_hard_regno
1167 && reg
->regno
< dst_hard_regno
+ dst_nregs
)
1174 *cand_id
->operand_loc
[cand
->nop
] = SET_DEST (set
);
1175 lra_update_insn_regno_info (cand
->insn
);
1176 bool ok_p
= lra_constrain_insn (cand
->insn
);
1179 rtx remat_pat
= copy_insn (PATTERN (cand
->insn
));
1182 emit_insn (remat_pat
);
1183 remat_insn
= get_insns ();
1185 if (recog_memoized (remat_insn
) < 0)
1187 cand_sp_offset
= cand_id
->sp_offset
;
1189 *cand_id
->operand_loc
[cand
->nop
] = saved_op
;
1190 lra_update_insn_regno_info (cand
->insn
);
1194 bitmap_clear (&temp_bitmap
);
1195 /* Update avail_cands (see analogous code for
1196 calculate_gen_cands). */
1197 for (iter
= 0; iter
< 2; iter
++)
1198 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
1201 if (reg
->type
!= OP_IN
1202 || find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1203 EXECUTE_IF_SET_IN_BITMAP (avail_cands
, 0, cid
, bi
)
1205 cand
= all_cands
[cid
];
1207 /* Ignore the reload insn. */
1208 if (src_regno
== cand
->reload_regno
1209 && dst_regno
== cand
->regno
)
1211 if (cand
->regno
== reg
->regno
1212 || reg_overlap_for_remat_p (reg
, cand
->insn
))
1213 bitmap_set_bit (&temp_bitmap
, cand
->index
);
1217 EXECUTE_IF_SET_IN_BITMAP (avail_cands
, 0, cid
, bi
)
1219 cand
= all_cands
[cid
];
1221 if (call_used_input_regno_present_p (cand
->insn
))
1222 bitmap_set_bit (&temp_bitmap
, cand
->index
);
1225 bitmap_and_compl_into (avail_cands
, &temp_bitmap
);
1227 /* Now see whether a candidate is made active or available
1229 cand
= insn_to_cand_activation
[INSN_UID (insn
)];
1231 bitmap_set_bit (active_cands
, cand
->index
);
1233 cand
= insn_to_cand
[INSN_UID (insn
)];
1236 bitmap_set_bit (avail_cands
, cand
->index
);
1237 if (cand
->reload_regno
== -1)
1238 bitmap_set_bit (active_cands
, cand
->index
);
1240 bitmap_clear_bit (active_cands
, cand
->index
);
1243 if (remat_insn
!= NULL
)
1245 poly_int64 sp_offset_change
= cand_sp_offset
- id
->sp_offset
;
1246 if (maybe_ne (sp_offset_change
, 0))
1247 change_sp_offset (remat_insn
, sp_offset_change
);
1248 update_scratch_ops (remat_insn
);
1249 lra_process_new_insns (insn
, remat_insn
, NULL
,
1250 "Inserting rematerialization insn");
1251 lra_set_insn_deleted (insn
);
1256 /* Update live hard regs: */
1257 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1258 if (reg
->type
== OP_IN
1259 && find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1261 if ((hard_regno
= get_hard_regs (reg
, nregs
)) < 0)
1263 for (i
= 0; i
< nregs
; i
++)
1264 CLEAR_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
);
1266 /* Process also hard regs (e.g. CC register) which are part
1267 of insn definition. */
1268 for (reg
= static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
1269 if (reg
->type
== OP_IN
1270 && find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1271 CLEAR_HARD_REG_BIT (live_hard_regs
, reg
->regno
);
1272 /* Inputs have been processed, now process outputs. */
1273 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1274 if (reg
->type
!= OP_IN
1275 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
1277 if ((hard_regno
= get_hard_regs (reg
, nregs
)) < 0)
1279 for (i
= 0; i
< nregs
; i
++)
1280 SET_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
);
1282 for (reg
= static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
1283 if (reg
->type
!= OP_IN
1284 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
1285 SET_HARD_REG_BIT (live_hard_regs
, reg
->regno
);
1293 /* Current number of rematerialization iteration. */
1294 int lra_rematerialization_iter
;
1296 /* Entry point of the rematerialization sub-pass. Return true if we
1297 did any rematerialization. */
1303 int max_regno
= max_reg_num ();
1305 if (! flag_lra_remat
)
1307 lra_rematerialization_iter
++;
1308 if (lra_rematerialization_iter
> LRA_MAX_REMATERIALIZATION_PASSES
)
1310 if (lra_dump_file
!= NULL
)
1311 fprintf (lra_dump_file
,
1312 "\n******** Rematerialization #%d: ********\n\n",
1313 lra_rematerialization_iter
);
1314 timevar_push (TV_LRA_REMAT
);
1315 insn_to_cand
= XCNEWVEC (cand_t
, get_max_uid ());
1316 insn_to_cand_activation
= XCNEWVEC (cand_t
, get_max_uid ());
1317 regno_cands
= XCNEWVEC (cand_t
, max_regno
);
1318 all_cands
.create (8000);
1319 call_used_regs_arr_len
= 0;
1320 for (int i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1321 if (call_used_regs
[i
])
1322 call_used_regs_arr
[call_used_regs_arr_len
++] = i
;
1323 initiate_cand_table ();
1324 create_remat_bb_data ();
1325 bitmap_initialize (&temp_bitmap
, ®_obstack
);
1326 bitmap_initialize (&subreg_regs
, ®_obstack
);
1327 calculate_local_reg_remat_bb_data ();
1329 calculate_livein_cands ();
1330 calculate_gen_cands ();
1331 bitmap_initialize (&all_blocks
, ®_obstack
);
1332 FOR_ALL_BB_FN (bb
, cfun
)
1333 bitmap_set_bit (&all_blocks
, bb
->index
);
1334 calculate_global_remat_bb_data ();
1335 dump_candidates_and_remat_bb_data ();
1336 result
= do_remat ();
1337 all_cands
.release ();
1338 bitmap_clear (&temp_bitmap
);
1339 bitmap_clear (&subreg_regs
);
1340 finish_remat_bb_data ();
1341 finish_cand_table ();
1342 bitmap_clear (&all_blocks
);
1344 free (insn_to_cand
);
1345 free (insn_to_cand_activation
);
1346 timevar_pop (TV_LRA_REMAT
);