1 /* Rematerialize pseudos values.
2 Copyright (C) 2014-2016 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"
68 /* Number of candidates for rematerialization. */
69 static unsigned int cands_num
;
71 /* The following is used for representation of call_used_reg_set in
72 form array whose elements are hard register numbers with nonzero bit
73 in CALL_USED_REG_SET. */
74 static int call_used_regs_arr_len
;
75 static int call_used_regs_arr
[FIRST_PSEUDO_REGISTER
];
77 /* Bitmap used for different calculations. */
78 static bitmap_head temp_bitmap
;
80 /* Registers accessed via subreg_p. */
81 static bitmap_head subreg_regs
;
83 typedef struct cand
*cand_t
;
84 typedef const struct cand
*const_cand_t
;
86 /* Insn candidates for rematerialization. The candidate insn should
87 have the following properies:
88 o no any memory (as access to memory is non-profitable)
89 o no INOUT regs (it means no non-paradoxical subreg of output reg)
90 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
91 o all other pseudos are with assigned hard regs. */
94 /* Index of the candidates in all_cands. */
96 /* The candidate insn. */
98 /* Insn pseudo regno for rematerialization. */
100 /* Non-negative if a reload pseudo is in the insn instead of the
101 pseudo for rematerialization. */
103 /* Number of the operand containing the regno or its reload
106 /* Next candidate for the same regno. */
107 cand_t next_regno_cand
;
110 /* Vector containing all candidates. */
111 static vec
<cand_t
> all_cands
;
112 /* Map: insn -> candidate representing it. It is null if the insn can
113 not be used for rematerialization. */
114 static cand_t
*insn_to_cand
;
115 /* A secondary map, for candidates that involve two insns, where the
116 second one makes the equivalence. The candidate must not be used
117 before seeing this activation insn. */
118 static cand_t
*insn_to_cand_activation
;
120 /* Map regno -> candidates can be used for the regno
121 rematerialization. */
122 static cand_t
*regno_cands
;
124 /* Data about basic blocks used for the rematerialization
128 /* Basic block about which the below data are. */
130 /* Registers changed in the basic block: */
131 bitmap_head changed_regs
;
132 /* Registers becoming dead in the BB. */
133 bitmap_head dead_regs
;
134 /* Cands present in the BB whose in/out regs are not changed after
135 the cands occurence and are not dead (except the reload
137 bitmap_head gen_cands
;
138 bitmap_head livein_cands
; /* cands whose inputs live at the BB start. */
139 bitmap_head pavin_cands
; /* cands partially available at BB entry. */
140 bitmap_head pavout_cands
; /* cands partially available at BB exit. */
141 bitmap_head avin_cands
; /* cands available at the entry of the BB. */
142 bitmap_head avout_cands
; /* cands available at the exit of the BB. */
145 /* Array for all BB data. Indexed by the corresponding BB index. */
146 typedef struct remat_bb_data
*remat_bb_data_t
;
148 /* Basic blocks for data flow problems -- all bocks except the special
150 static bitmap_head all_blocks
;
152 /* All basic block data are referred through the following array. */
153 static remat_bb_data_t remat_bb_data
;
155 /* Two small functions for access to the bb data. */
156 static inline remat_bb_data_t
157 get_remat_bb_data (basic_block bb
)
159 return &remat_bb_data
[(bb
)->index
];
162 static inline remat_bb_data_t
163 get_remat_bb_data_by_index (int index
)
165 return &remat_bb_data
[index
];
170 /* Recursive hash function for RTL X. */
183 val
+= (int) code
+ 4095;
185 /* Some RTL can be compared nonrecursively. */
189 return val
+ REGNO (x
);
192 return iterative_hash_object (XEXP (x
, 0), val
);
195 return iterative_hash_object (XSTR (x
, 0), val
);
207 /* Hash the elements. */
208 fmt
= GET_RTX_FORMAT (code
);
209 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
224 val
+= XVECLEN (x
, i
);
226 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
227 val
+= rtx_hash (XVECEXP (x
, i
, j
));
231 val
+= rtx_hash (XEXP (x
, i
));
236 val
+= htab_hash_string (XSTR (x
, i
));
244 /* It is believed that rtx's at this level will never
245 contain anything but integers and other rtx's, except for
246 within LABEL_REFs and SYMBOL_REFs. */
256 /* Hash table for the candidates. Different insns (e.g. structurally
257 the same insns or even insns with different unused output regs) can
258 be represented by the same candidate in the table. */
259 static htab_t cand_table
;
261 /* Hash function for candidate CAND. */
263 cand_hash (const void *cand
)
265 const_cand_t c
= (const_cand_t
) cand
;
266 lra_insn_recog_data_t id
= lra_get_insn_recog_data (c
->insn
);
267 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
268 int nops
= static_id
->n_operands
;
271 for (int i
= 0; i
< nops
; i
++)
273 hash
= iterative_hash_object (c
->regno
, hash
);
274 else if (static_id
->operand
[i
].type
== OP_IN
)
275 hash
= iterative_hash_object (*id
->operand_loc
[i
], hash
);
279 /* Equal function for candidates CAND1 and CAND2. They are equal if
280 the corresponding candidate insns have the same code, the same
281 regno for rematerialization, the same input operands. */
283 cand_eq_p (const void *cand1
, const void *cand2
)
285 const_cand_t c1
= (const_cand_t
) cand1
;
286 const_cand_t c2
= (const_cand_t
) cand2
;
287 lra_insn_recog_data_t id1
= lra_get_insn_recog_data (c1
->insn
);
288 lra_insn_recog_data_t id2
= lra_get_insn_recog_data (c2
->insn
);
289 struct lra_static_insn_data
*static_id1
= id1
->insn_static_data
;
290 int nops
= static_id1
->n_operands
;
292 if (c1
->regno
!= c2
->regno
293 || INSN_CODE (c1
->insn
) < 0
294 || INSN_CODE (c1
->insn
) != INSN_CODE (c2
->insn
))
296 gcc_assert (c1
->nop
== c2
->nop
);
297 for (int i
= 0; i
< nops
; i
++)
298 if (i
!= c1
->nop
&& static_id1
->operand
[i
].type
== OP_IN
299 && *id1
->operand_loc
[i
] != *id2
->operand_loc
[i
])
304 /* Insert candidate CAND into the table if it is not there yet.
305 Return candidate which is in the table. */
307 insert_cand (cand_t cand
)
311 entry_ptr
= htab_find_slot (cand_table
, cand
, INSERT
);
312 if (*entry_ptr
== NULL
)
313 *entry_ptr
= (void *) cand
;
314 return (cand_t
) *entry_ptr
;
317 /* Free candidate CAND memory. */
319 free_cand (void *cand
)
324 /* Initiate the candidate table. */
326 initiate_cand_table (void)
328 cand_table
= htab_create (8000, cand_hash
, cand_eq_p
,
329 (htab_del
) free_cand
);
332 /* Finish the candidate table. */
334 finish_cand_table (void)
336 htab_delete (cand_table
);
341 /* Return true if X contains memory or some UNSPEC. We can not just
342 check insn operands as memory or unspec might be not an operand
343 itself but contain an operand. Insn with memory access is not
344 profitable for rematerialization. Rematerialization of UNSPEC
345 might result in wrong code generation as the UNPEC effect is
346 unknown (e.g. generating a label). */
348 bad_for_rematerialization_p (rtx x
)
354 if (MEM_P (x
) || GET_CODE (x
) == UNSPEC
|| GET_CODE (x
) == UNSPEC_VOLATILE
)
357 fmt
= GET_RTX_FORMAT (code
);
358 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
362 if (bad_for_rematerialization_p (XEXP (x
, i
)))
365 else if (fmt
[i
] == 'E')
367 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
368 if (bad_for_rematerialization_p (XVECEXP (x
, i
, j
)))
375 /* If INSN can not be used for rematerialization, return negative
376 value. If INSN can be considered as a candidate for
377 rematerialization, return value which is the operand number of the
378 pseudo for which the insn can be used for rematerialization. Here
379 we consider the insns without any memory, spilled pseudo (except
380 for the rematerialization pseudo), or dying or unused regs. */
382 operand_to_remat (rtx_insn
*insn
)
384 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
385 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
386 struct lra_insn_reg
*reg
, *found_reg
= NULL
;
388 /* Don't rematerialize insns which can change PC. */
389 if (JUMP_P (insn
) || CALL_P (insn
))
391 /* First find a pseudo which can be rematerialized. */
392 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
394 /* True FRAME_POINTER_NEEDED might be because we can not follow
395 changing sp offsets, e.g. alloca is used. If the insn contains
396 stack pointer in such case, we can not rematerialize it as we
397 can not know sp offset at a rematerialization place. */
398 if (reg
->regno
== STACK_POINTER_REGNUM
&& frame_pointer_needed
)
400 else if (reg
->type
== OP_OUT
&& ! reg
->subreg_p
401 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
403 /* We permits only one spilled reg. */
404 if (found_reg
!= NULL
)
408 /* IRA calculates conflicts separately for subregs of two words
409 pseudo. Even if the pseudo lives, e.g. one its subreg can be
410 used lately, another subreg hard register can be already used
411 for something else. In such case, it is not safe to
412 rematerialize the insn. */
413 if (reg
->regno
>= FIRST_PSEUDO_REGISTER
414 && bitmap_bit_p (&subreg_regs
, reg
->regno
))
417 /* Don't allow hard registers to be rematerialized. */
418 if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
421 if (found_reg
== NULL
)
423 if (found_reg
->regno
< FIRST_PSEUDO_REGISTER
)
425 if (bad_for_rematerialization_p (PATTERN (insn
)))
427 /* Check the other regs are not spilled. */
428 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
429 if (found_reg
== reg
)
431 else if (reg
->type
== OP_INOUT
)
433 else if (reg
->regno
>= FIRST_PSEUDO_REGISTER
434 && reg_renumber
[reg
->regno
] < 0)
435 /* Another spilled reg. */
437 else if (reg
->type
== OP_IN
)
439 if (find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
440 /* We don't want to make live ranges longer. */
442 /* Check that there is no output reg as the input one. */
443 for (struct lra_insn_reg
*reg2
= id
->regs
;
446 if (reg2
->type
== OP_OUT
&& reg
->regno
== reg2
->regno
)
448 if (reg
->regno
< FIRST_PSEUDO_REGISTER
)
449 for (struct lra_insn_reg
*reg2
= static_id
->hard_regs
;
452 if (reg2
->type
== OP_OUT
453 && reg
->regno
<= reg2
->regno
456 + hard_regno_nregs
[reg
->regno
][reg
->biggest_mode
])))
459 /* Find the rematerialization operand. */
460 int nop
= static_id
->n_operands
;
461 for (int i
= 0; i
< nop
; i
++)
462 if (REG_P (*id
->operand_loc
[i
])
463 && (int) REGNO (*id
->operand_loc
[i
]) == found_reg
->regno
)
468 /* Create candidate for INSN with rematerialization operand NOP and
469 REGNO. Insert the candidate into the table and set up the
470 corresponding INSN_TO_CAND element. */
472 create_cand (rtx_insn
*insn
, int nop
, int regno
, rtx_insn
*activation
= NULL
)
474 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
475 rtx reg
= *id
->operand_loc
[nop
];
476 gcc_assert (REG_P (reg
));
477 int op_regno
= REGNO (reg
);
478 gcc_assert (op_regno
>= FIRST_PSEUDO_REGISTER
);
479 cand_t cand
= XNEW (struct cand
);
483 cand
->reload_regno
= op_regno
== regno
? -1 : op_regno
;
484 gcc_assert (cand
->regno
>= 0);
485 cand_t cand_in_table
= insert_cand (cand
);
486 insn_to_cand
[INSN_UID (insn
)] = cand_in_table
;
487 if (cand
!= cand_in_table
)
492 cand
->index
= all_cands
.length ();
493 all_cands
.safe_push (cand
);
494 cand
->next_regno_cand
= regno_cands
[cand
->regno
];
495 regno_cands
[cand
->regno
] = cand
;
498 insn_to_cand_activation
[INSN_UID (activation
)] = cand_in_table
;
501 /* Create rematerialization candidates (inserting them into the
507 struct potential_cand
512 struct potential_cand
*regno_potential_cand
;
514 /* Create candidates. */
515 regno_potential_cand
= XCNEWVEC (struct potential_cand
, max_reg_num ());
516 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
517 if (NONDEBUG_INSN_P (insn
))
519 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
521 rtx set
= single_set (insn
);
524 /* See if this is an output reload for a previous insn. */
526 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
528 rtx dstreg
= SET_DEST (set
);
529 int src_regno
= REGNO (SET_SRC (set
));
530 int dst_regno
= REGNO (dstreg
);
531 rtx_insn
*insn2
= regno_potential_cand
[src_regno
].insn
;
534 && dst_regno
>= FIRST_PSEUDO_REGISTER
535 && reg_renumber
[dst_regno
] < 0
536 && BLOCK_FOR_INSN (insn2
) == BLOCK_FOR_INSN (insn
))
538 create_cand (insn2
, regno_potential_cand
[src_regno
].nop
,
544 nop
= operand_to_remat (insn
);
547 gcc_assert (REG_P (*id
->operand_loc
[nop
]));
548 int regno
= REGNO (*id
->operand_loc
[nop
]);
549 gcc_assert (regno
>= FIRST_PSEUDO_REGISTER
);
550 /* If we're setting an unrenumbered pseudo, make a candidate immediately.
551 If it's an output reload register, save it for later; the code above
552 looks for output reload insns later on. */
553 if (reg_renumber
[regno
] < 0)
554 create_cand (insn
, nop
, regno
);
555 else if (regno
>= lra_constraint_new_regno_start
)
557 regno_potential_cand
[regno
].insn
= insn
;
558 regno_potential_cand
[regno
].nop
= nop
;
564 for (struct lra_insn_reg
*reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
565 if (reg
->type
!= OP_IN
&& reg
->regno
!= keep_regno
566 && reg
->regno
>= FIRST_PSEUDO_REGISTER
)
567 regno_potential_cand
[reg
->regno
].insn
= NULL
;
569 cands_num
= all_cands
.length ();
570 free (regno_potential_cand
);
575 /* Create and initialize BB data. */
577 create_remat_bb_data (void)
580 remat_bb_data_t bb_info
;
582 remat_bb_data
= XNEWVEC (struct remat_bb_data
,
583 last_basic_block_for_fn (cfun
));
584 FOR_ALL_BB_FN (bb
, cfun
)
586 gcc_checking_assert (bb
->index
>= 0
587 && bb
->index
< last_basic_block_for_fn (cfun
));
588 bb_info
= get_remat_bb_data (bb
);
590 bitmap_initialize (&bb_info
->changed_regs
, ®_obstack
);
591 bitmap_initialize (&bb_info
->dead_regs
, ®_obstack
);
592 bitmap_initialize (&bb_info
->gen_cands
, ®_obstack
);
593 bitmap_initialize (&bb_info
->livein_cands
, ®_obstack
);
594 bitmap_initialize (&bb_info
->pavin_cands
, ®_obstack
);
595 bitmap_initialize (&bb_info
->pavout_cands
, ®_obstack
);
596 bitmap_initialize (&bb_info
->avin_cands
, ®_obstack
);
597 bitmap_initialize (&bb_info
->avout_cands
, ®_obstack
);
601 /* Dump all candidates to DUMP_FILE. */
603 dump_cands (FILE *dump_file
)
608 fprintf (dump_file
, "\nCands:\n");
609 for (i
= 0; i
< (int) cands_num
; i
++)
612 fprintf (dump_file
, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
613 i
, cand
->nop
, cand
->regno
, cand
->reload_regno
);
614 print_inline_rtx (dump_file
, cand
->insn
, 6);
615 fprintf (dump_file
, "\n");
619 /* Dump all candidates and BB data. */
621 dump_candidates_and_remat_bb_data (void)
625 if (lra_dump_file
== NULL
)
627 dump_cands (lra_dump_file
);
628 FOR_EACH_BB_FN (bb
, cfun
)
630 fprintf (lra_dump_file
, "\nBB %d:\n", bb
->index
);
632 fprintf (lra_dump_file
, " register live in:");
633 dump_regset (df_get_live_in (bb
), lra_dump_file
);
634 putc ('\n', lra_dump_file
);
636 fprintf (lra_dump_file
, " register live out:");
637 dump_regset (df_get_live_out (bb
), lra_dump_file
);
638 putc ('\n', lra_dump_file
);
639 /* Changed/dead regs: */
640 fprintf (lra_dump_file
, " changed regs:");
641 dump_regset (&get_remat_bb_data (bb
)->changed_regs
, lra_dump_file
);
642 putc ('\n', lra_dump_file
);
643 fprintf (lra_dump_file
, " dead regs:");
644 dump_regset (&get_remat_bb_data (bb
)->dead_regs
, lra_dump_file
);
645 putc ('\n', lra_dump_file
);
646 lra_dump_bitmap_with_title ("cands generated in BB",
647 &get_remat_bb_data (bb
)->gen_cands
, bb
->index
);
648 lra_dump_bitmap_with_title ("livein cands in BB",
649 &get_remat_bb_data (bb
)->livein_cands
, bb
->index
);
650 lra_dump_bitmap_with_title ("pavin cands in BB",
651 &get_remat_bb_data (bb
)->pavin_cands
, bb
->index
);
652 lra_dump_bitmap_with_title ("pavout cands in BB",
653 &get_remat_bb_data (bb
)->pavout_cands
, bb
->index
);
654 lra_dump_bitmap_with_title ("avin cands in BB",
655 &get_remat_bb_data (bb
)->avin_cands
, bb
->index
);
656 lra_dump_bitmap_with_title ("avout cands in BB",
657 &get_remat_bb_data (bb
)->avout_cands
, bb
->index
);
659 fprintf (lra_dump_file
, "subreg regs:");
660 dump_regset (&subreg_regs
, lra_dump_file
);
661 putc ('\n', lra_dump_file
);
664 /* Free all BB data. */
666 finish_remat_bb_data (void)
670 FOR_EACH_BB_FN (bb
, cfun
)
672 bitmap_clear (&get_remat_bb_data (bb
)->avout_cands
);
673 bitmap_clear (&get_remat_bb_data (bb
)->avin_cands
);
674 bitmap_clear (&get_remat_bb_data (bb
)->pavout_cands
);
675 bitmap_clear (&get_remat_bb_data (bb
)->pavin_cands
);
676 bitmap_clear (&get_remat_bb_data (bb
)->livein_cands
);
677 bitmap_clear (&get_remat_bb_data (bb
)->gen_cands
);
678 bitmap_clear (&get_remat_bb_data (bb
)->dead_regs
);
679 bitmap_clear (&get_remat_bb_data (bb
)->changed_regs
);
681 free (remat_bb_data
);
686 /* Update changed_regs, dead_regs, subreg_regs of BB from INSN. */
688 set_bb_regs (basic_block bb
, rtx_insn
*insn
)
690 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
691 remat_bb_data_t bb_info
= get_remat_bb_data (bb
);
692 struct lra_insn_reg
*reg
;
694 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
696 unsigned regno
= reg
->regno
;
697 if (reg
->type
!= OP_IN
)
698 bitmap_set_bit (&bb_info
->changed_regs
, regno
);
699 else if (find_regno_note (insn
, REG_DEAD
, regno
) != NULL
)
700 bitmap_set_bit (&bb_info
->dead_regs
, regno
);
701 if (regno
>= FIRST_PSEUDO_REGISTER
&& reg
->subreg_p
)
702 bitmap_set_bit (&subreg_regs
, regno
);
705 for (int i
= 0; i
< call_used_regs_arr_len
; i
++)
706 bitmap_set_bit (&get_remat_bb_data (bb
)->dead_regs
,
707 call_used_regs_arr
[i
]);
710 /* Calculate changed_regs and dead_regs for each BB. */
712 calculate_local_reg_remat_bb_data (void)
717 FOR_EACH_BB_FN (bb
, cfun
)
718 FOR_BB_INSNS (bb
, insn
)
719 if (NONDEBUG_INSN_P (insn
))
720 set_bb_regs (bb
, insn
);
725 /* Return true if REG overlaps an input operand of INSN. */
727 reg_overlap_for_remat_p (lra_insn_reg
*reg
, rtx_insn
*insn
)
730 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
731 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
732 unsigned regno
= reg
->regno
;
735 if (regno
>= FIRST_PSEUDO_REGISTER
&& reg_renumber
[regno
] >= 0)
736 regno
= reg_renumber
[regno
];
737 if (regno
>= FIRST_PSEUDO_REGISTER
)
740 nregs
= hard_regno_nregs
[regno
][reg
->biggest_mode
];
742 struct lra_insn_reg
*reg2
;
744 for (iter
= 0; iter
< 2; iter
++)
745 for (reg2
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
749 if (reg2
->type
!= OP_IN
)
751 unsigned regno2
= reg2
->regno
;
754 if (regno2
>= FIRST_PSEUDO_REGISTER
&& reg_renumber
[regno2
] >= 0)
755 regno2
= reg_renumber
[regno2
];
756 if (regno
>= FIRST_PSEUDO_REGISTER
)
759 nregs2
= hard_regno_nregs
[regno2
][reg
->biggest_mode
];
761 if ((regno2
+ nregs2
- 1 >= regno
&& regno2
< regno
+ nregs
)
762 || (regno
+ nregs
- 1 >= regno2
&& regno
< regno2
+ nregs2
))
768 /* Return true if a call used register is an input operand of INSN. */
770 call_used_input_regno_present_p (rtx_insn
*insn
)
773 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
774 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
775 struct lra_insn_reg
*reg
;
777 for (iter
= 0; iter
< 2; iter
++)
778 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
781 if (reg
->type
== OP_IN
&& reg
->regno
<= FIRST_PSEUDO_REGISTER
782 && TEST_HARD_REG_BIT (call_used_reg_set
, reg
->regno
))
787 /* Calculate livein_cands for each BB. */
789 calculate_livein_cands (void)
793 FOR_EACH_BB_FN (bb
, cfun
)
795 bitmap livein_regs
= df_get_live_in (bb
);
796 bitmap livein_cands
= &get_remat_bb_data (bb
)->livein_cands
;
797 for (unsigned int i
= 0; i
< cands_num
; i
++)
799 cand_t cand
= all_cands
[i
];
800 lra_insn_recog_data_t id
= lra_get_insn_recog_data (cand
->insn
);
801 struct lra_insn_reg
*reg
;
803 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
804 if (reg
->type
== OP_IN
&& ! bitmap_bit_p (livein_regs
, reg
->regno
))
807 bitmap_set_bit (livein_cands
, i
);
812 /* Calculate gen_cands for each BB. */
814 calculate_gen_cands (void)
818 bitmap_head gen_insns
;
821 bitmap_initialize (&gen_insns
, ®_obstack
);
822 FOR_EACH_BB_FN (bb
, cfun
)
824 gen_cands
= &get_remat_bb_data (bb
)->gen_cands
;
825 bitmap_clear (&gen_insns
);
826 FOR_BB_INSNS (bb
, insn
)
829 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
830 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
831 struct lra_insn_reg
*reg
;
837 int src_regno
= -1, dst_regno
= -1;
839 if ((set
= single_set (insn
)) != NULL
840 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
842 src_regno
= REGNO (SET_SRC (set
));
843 dst_regno
= REGNO (SET_DEST (set
));
846 /* Update gen_cands: */
847 bitmap_clear (&temp_bitmap
);
848 for (iter
= 0; iter
< 2; iter
++)
849 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
852 if (reg
->type
!= OP_IN
853 || find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
854 EXECUTE_IF_SET_IN_BITMAP (&gen_insns
, 0, uid
, bi
)
856 rtx_insn
*insn2
= lra_insn_recog_data
[uid
]->insn
;
858 cand
= insn_to_cand
[INSN_UID (insn2
)];
859 gcc_assert (cand
!= NULL
);
860 /* Ignore the reload insn. */
861 if (src_regno
== cand
->reload_regno
862 && dst_regno
== cand
->regno
)
864 if (cand
->regno
== reg
->regno
865 || reg_overlap_for_remat_p (reg
, insn2
))
867 bitmap_clear_bit (gen_cands
, cand
->index
);
868 bitmap_set_bit (&temp_bitmap
, uid
);
873 EXECUTE_IF_SET_IN_BITMAP (&gen_insns
, 0, uid
, bi
)
875 rtx_insn
*insn2
= lra_insn_recog_data
[uid
]->insn
;
877 cand
= insn_to_cand
[INSN_UID (insn2
)];
878 gcc_assert (cand
!= NULL
);
879 if (call_used_input_regno_present_p (insn2
))
881 bitmap_clear_bit (gen_cands
, cand
->index
);
882 bitmap_set_bit (&temp_bitmap
, uid
);
885 bitmap_and_compl_into (&gen_insns
, &temp_bitmap
);
887 cand
= insn_to_cand
[INSN_UID (insn
)];
890 bitmap_set_bit (gen_cands
, cand
->index
);
891 bitmap_set_bit (&gen_insns
, INSN_UID (insn
));
895 bitmap_clear (&gen_insns
);
900 /* The common transfer function used by the DF equation solver to
901 propagate (partial) availability info BB_IN to BB_OUT through block
902 with BB_INDEX according to the following equation:
904 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
907 cand_trans_fun (int bb_index
, bitmap bb_in
, bitmap bb_out
)
909 remat_bb_data_t bb_info
;
910 bitmap bb_livein
, bb_changed_regs
, bb_dead_regs
;
914 bb_info
= get_remat_bb_data_by_index (bb_index
);
915 bb_livein
= &bb_info
->livein_cands
;
916 bb_changed_regs
= &bb_info
->changed_regs
;
917 bb_dead_regs
= &bb_info
->dead_regs
;
918 /* Calculate killed avin cands -- cands whose regs are changed or
919 becoming dead in the BB. We calculate it here as we hope that
920 repeated calculations are compensated by smaller size of BB_IN in
921 comparison with all candidates number. */
922 bitmap_clear (&temp_bitmap
);
923 EXECUTE_IF_SET_IN_BITMAP (bb_in
, 0, cid
, bi
)
925 cand_t cand
= all_cands
[cid
];
926 lra_insn_recog_data_t id
= lra_get_insn_recog_data (cand
->insn
);
927 struct lra_insn_reg
*reg
;
929 if (! bitmap_bit_p (bb_livein
, cid
))
931 bitmap_set_bit (&temp_bitmap
, cid
);
934 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
935 /* Ignore all outputs which are not the regno for
936 rematerialization. */
937 if (reg
->type
== OP_OUT
&& reg
->regno
!= cand
->regno
)
939 else if (bitmap_bit_p (bb_changed_regs
, reg
->regno
)
940 || bitmap_bit_p (bb_dead_regs
, reg
->regno
))
942 bitmap_set_bit (&temp_bitmap
, cid
);
945 /* Check regno for rematerialization. */
946 if (bitmap_bit_p (bb_changed_regs
, cand
->regno
)
947 || bitmap_bit_p (bb_dead_regs
, cand
->regno
))
948 bitmap_set_bit (&temp_bitmap
, cid
);
950 return bitmap_ior_and_compl (bb_out
,
951 &bb_info
->gen_cands
, bb_in
, &temp_bitmap
);
956 /* The transfer function used by the DF equation solver to propagate
957 partial candidate availability info through block with BB_INDEX
958 according to the following equation:
960 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
963 cand_pav_trans_fun (int bb_index
)
965 remat_bb_data_t bb_info
;
967 bb_info
= get_remat_bb_data_by_index (bb_index
);
968 return cand_trans_fun (bb_index
, &bb_info
->pavin_cands
,
969 &bb_info
->pavout_cands
);
972 /* The confluence function used by the DF equation solver to set up
973 cand_pav info for a block BB without predecessor. */
975 cand_pav_con_fun_0 (basic_block bb
)
977 bitmap_clear (&get_remat_bb_data (bb
)->pavin_cands
);
980 /* The confluence function used by the DF equation solver to propagate
981 partial candidate availability info from predecessor to successor
982 on edge E (pred->bb) according to the following equation:
984 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
987 cand_pav_con_fun_n (edge e
)
989 basic_block pred
= e
->src
;
990 basic_block bb
= e
->dest
;
991 remat_bb_data_t bb_info
;
992 bitmap bb_pavin
, pred_pavout
;
994 bb_info
= get_remat_bb_data (bb
);
995 bb_pavin
= &bb_info
->pavin_cands
;
996 pred_pavout
= &get_remat_bb_data (pred
)->pavout_cands
;
997 return bitmap_ior_into (bb_pavin
, pred_pavout
);
1002 /* The transfer function used by the DF equation solver to propagate
1003 candidate availability info through block with BB_INDEX according
1004 to the following equation:
1006 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
1009 cand_av_trans_fun (int bb_index
)
1011 remat_bb_data_t bb_info
;
1013 bb_info
= get_remat_bb_data_by_index (bb_index
);
1014 return cand_trans_fun (bb_index
, &bb_info
->avin_cands
,
1015 &bb_info
->avout_cands
);
1018 /* The confluence function used by the DF equation solver to set up
1019 cand_av info for a block BB without predecessor. */
1021 cand_av_con_fun_0 (basic_block bb
)
1023 bitmap_clear (&get_remat_bb_data (bb
)->avin_cands
);
1026 /* The confluence function used by the DF equation solver to propagate
1027 cand_av info from predecessor to successor on edge E (pred->bb)
1028 according to the following equation:
1030 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
1033 cand_av_con_fun_n (edge e
)
1035 basic_block pred
= e
->src
;
1036 basic_block bb
= e
->dest
;
1037 remat_bb_data_t bb_info
;
1038 bitmap bb_avin
, pred_avout
;
1040 bb_info
= get_remat_bb_data (bb
);
1041 bb_avin
= &bb_info
->avin_cands
;
1042 pred_avout
= &get_remat_bb_data (pred
)->avout_cands
;
1043 return bitmap_and_into (bb_avin
, pred_avout
);
1046 /* Calculate available candidates for each BB. */
1048 calculate_global_remat_bb_data (void)
1053 (DF_FORWARD
, NULL
, cand_pav_con_fun_0
, cand_pav_con_fun_n
,
1054 cand_pav_trans_fun
, &all_blocks
,
1055 df_get_postorder (DF_FORWARD
), df_get_n_blocks (DF_FORWARD
));
1056 /* Initialize avin by pavin. */
1057 FOR_EACH_BB_FN (bb
, cfun
)
1058 bitmap_copy (&get_remat_bb_data (bb
)->avin_cands
,
1059 &get_remat_bb_data (bb
)->pavin_cands
);
1061 (DF_FORWARD
, NULL
, cand_av_con_fun_0
, cand_av_con_fun_n
,
1062 cand_av_trans_fun
, &all_blocks
,
1063 df_get_postorder (DF_FORWARD
), df_get_n_blocks (DF_FORWARD
));
1068 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
1070 change_sp_offset (rtx_insn
*insns
, HOST_WIDE_INT sp_offset
)
1072 for (rtx_insn
*insn
= insns
; insn
!= NULL
; insn
= NEXT_INSN (insn
))
1073 eliminate_regs_in_insn (insn
, false, false, sp_offset
);
1076 /* Return start hard register of REG (can be a hard or a pseudo reg)
1077 or -1 (if it is a spilled pseudo). Return number of hard registers
1078 occupied by REG through parameter NREGS if the start hard reg is
1081 get_hard_regs (struct lra_insn_reg
*reg
, int &nregs
)
1083 int regno
= reg
->regno
;
1084 int hard_regno
= regno
< FIRST_PSEUDO_REGISTER
? regno
: reg_renumber
[regno
];
1086 if (hard_regno
>= 0)
1087 nregs
= hard_regno_nregs
[hard_regno
][reg
->biggest_mode
];
1091 /* Make copy of and register scratch pseudos in rematerialized insn
1094 update_scratch_ops (rtx_insn
*remat_insn
)
1096 lra_insn_recog_data_t id
= lra_get_insn_recog_data (remat_insn
);
1097 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
1098 for (int i
= 0; i
< static_id
->n_operands
; i
++)
1100 rtx
*loc
= id
->operand_loc
[i
];
1103 int regno
= REGNO (*loc
);
1104 if (! lra_former_scratch_p (regno
))
1106 *loc
= lra_create_new_reg (GET_MODE (*loc
), *loc
,
1107 lra_get_allocno_class (regno
),
1108 "scratch pseudo copy");
1109 lra_register_new_scratch_op (remat_insn
, i
);
1114 /* Insert rematerialization insns using the data-flow data calculated
1121 bitmap_head avail_cands
;
1122 bitmap_head active_cands
;
1123 bool changed_p
= false;
1124 /* Living hard regs and hard registers of living pseudos. */
1125 HARD_REG_SET live_hard_regs
;
1127 bitmap_initialize (&avail_cands
, ®_obstack
);
1128 bitmap_initialize (&active_cands
, ®_obstack
);
1129 FOR_EACH_BB_FN (bb
, cfun
)
1131 REG_SET_TO_HARD_REG_SET (live_hard_regs
, df_get_live_out (bb
));
1132 bitmap_and (&avail_cands
, &get_remat_bb_data (bb
)->avin_cands
,
1133 &get_remat_bb_data (bb
)->livein_cands
);
1134 /* Activating insns are always in the same block as their corresponding
1135 remat insn, so at the start of a block the two bitsets are equal. */
1136 bitmap_copy (&active_cands
, &avail_cands
);
1137 FOR_BB_INSNS (bb
, insn
)
1139 if (!NONDEBUG_INSN_P (insn
))
1142 lra_insn_recog_data_t id
= lra_get_insn_recog_data (insn
);
1143 struct lra_static_insn_data
*static_id
= id
->insn_static_data
;
1144 struct lra_insn_reg
*reg
;
1150 int src_regno
= -1, dst_regno
= -1;
1152 if ((set
= single_set (insn
)) != NULL
1153 && REG_P (SET_SRC (set
)) && REG_P (SET_DEST (set
)))
1155 src_regno
= REGNO (SET_SRC (set
));
1156 dst_regno
= REGNO (SET_DEST (set
));
1160 /* Check possibility of rematerialization (hard reg or
1161 unpsilled pseudo <- spilled pseudo): */
1162 if (dst_regno
>= 0 && src_regno
>= FIRST_PSEUDO_REGISTER
1163 && reg_renumber
[src_regno
] < 0
1164 && (dst_regno
< FIRST_PSEUDO_REGISTER
1165 || reg_renumber
[dst_regno
] >= 0))
1167 for (cand
= regno_cands
[src_regno
];
1169 cand
= cand
->next_regno_cand
)
1170 if (bitmap_bit_p (&avail_cands
, cand
->index
)
1171 && bitmap_bit_p (&active_cands
, cand
->index
))
1174 int i
, hard_regno
, nregs
;
1175 rtx_insn
*remat_insn
= NULL
;
1176 HOST_WIDE_INT cand_sp_offset
= 0;
1179 lra_insn_recog_data_t cand_id
1180 = lra_get_insn_recog_data (cand
->insn
);
1181 struct lra_static_insn_data
*static_cand_id
1182 = cand_id
->insn_static_data
;
1183 rtx saved_op
= *cand_id
->operand_loc
[cand
->nop
];
1185 /* Check clobbers do not kill something living. */
1186 gcc_assert (REG_P (saved_op
));
1187 int ignore_regno
= REGNO (saved_op
);
1189 for (reg
= cand_id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1190 if (reg
->type
!= OP_IN
&& reg
->regno
!= ignore_regno
)
1192 hard_regno
= get_hard_regs (reg
, nregs
);
1193 gcc_assert (hard_regno
>= 0);
1194 for (i
= 0; i
< nregs
; i
++)
1195 if (TEST_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
))
1203 for (reg
= static_cand_id
->hard_regs
;
1206 if (reg
->type
!= OP_IN
1207 && TEST_HARD_REG_BIT (live_hard_regs
, reg
->regno
))
1213 *cand_id
->operand_loc
[cand
->nop
] = SET_DEST (set
);
1214 lra_update_insn_regno_info (cand
->insn
);
1215 bool ok_p
= lra_constrain_insn (cand
->insn
);
1218 rtx remat_pat
= copy_insn (PATTERN (cand
->insn
));
1221 emit_insn (remat_pat
);
1222 remat_insn
= get_insns ();
1224 if (recog_memoized (remat_insn
) < 0)
1226 cand_sp_offset
= cand_id
->sp_offset
;
1228 *cand_id
->operand_loc
[cand
->nop
] = saved_op
;
1229 lra_update_insn_regno_info (cand
->insn
);
1233 bitmap_clear (&temp_bitmap
);
1234 /* Update avail_cands (see analogous code for
1235 calculate_gen_cands). */
1236 for (iter
= 0; iter
< 2; iter
++)
1237 for (reg
= (iter
== 0 ? id
->regs
: static_id
->hard_regs
);
1240 if (reg
->type
!= OP_IN
1241 || find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1242 EXECUTE_IF_SET_IN_BITMAP (&avail_cands
, 0, cid
, bi
)
1244 cand
= all_cands
[cid
];
1246 /* Ignore the reload insn. */
1247 if (src_regno
== cand
->reload_regno
1248 && dst_regno
== cand
->regno
)
1250 if (cand
->regno
== reg
->regno
1251 || reg_overlap_for_remat_p (reg
, cand
->insn
))
1252 bitmap_set_bit (&temp_bitmap
, cand
->index
);
1256 EXECUTE_IF_SET_IN_BITMAP (&avail_cands
, 0, cid
, bi
)
1258 cand
= all_cands
[cid
];
1260 if (call_used_input_regno_present_p (cand
->insn
))
1261 bitmap_set_bit (&temp_bitmap
, cand
->index
);
1264 bitmap_and_compl_into (&avail_cands
, &temp_bitmap
);
1266 /* Now see whether a candidate is made active or available
1268 cand
= insn_to_cand_activation
[INSN_UID (insn
)];
1270 bitmap_set_bit (&active_cands
, cand
->index
);
1272 cand
= insn_to_cand
[INSN_UID (insn
)];
1275 bitmap_set_bit (&avail_cands
, cand
->index
);
1276 if (cand
->reload_regno
== -1)
1277 bitmap_set_bit (&active_cands
, cand
->index
);
1279 bitmap_clear_bit (&active_cands
, cand
->index
);
1282 if (remat_insn
!= NULL
)
1284 HOST_WIDE_INT sp_offset_change
= cand_sp_offset
- id
->sp_offset
;
1285 if (sp_offset_change
!= 0)
1286 change_sp_offset (remat_insn
, sp_offset_change
);
1287 update_scratch_ops (remat_insn
);
1288 lra_process_new_insns (insn
, remat_insn
, NULL
,
1289 "Inserting rematerialization insn");
1290 lra_set_insn_deleted (insn
);
1295 /* Update live hard regs: */
1296 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1297 if (reg
->type
== OP_IN
1298 && find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1300 if ((hard_regno
= get_hard_regs (reg
, nregs
)) < 0)
1302 for (i
= 0; i
< nregs
; i
++)
1303 CLEAR_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
);
1305 /* Process also hard regs (e.g. CC register) which are part
1306 of insn definition. */
1307 for (reg
= static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
1308 if (reg
->type
== OP_IN
1309 && find_regno_note (insn
, REG_DEAD
, reg
->regno
) != NULL
)
1310 CLEAR_HARD_REG_BIT (live_hard_regs
, reg
->regno
);
1311 /* Inputs have been processed, now process outputs. */
1312 for (reg
= id
->regs
; reg
!= NULL
; reg
= reg
->next
)
1313 if (reg
->type
!= OP_IN
1314 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
1316 if ((hard_regno
= get_hard_regs (reg
, nregs
)) < 0)
1318 for (i
= 0; i
< nregs
; i
++)
1319 SET_HARD_REG_BIT (live_hard_regs
, hard_regno
+ i
);
1321 for (reg
= static_id
->hard_regs
; reg
!= NULL
; reg
= reg
->next
)
1322 if (reg
->type
!= OP_IN
1323 && find_regno_note (insn
, REG_UNUSED
, reg
->regno
) == NULL
)
1324 SET_HARD_REG_BIT (live_hard_regs
, reg
->regno
);
1327 bitmap_clear (&avail_cands
);
1328 bitmap_clear (&active_cands
);
1334 /* Current number of rematerialization iteration. */
1335 int lra_rematerialization_iter
;
1337 /* Entry point of the rematerialization sub-pass. Return true if we
1338 did any rematerialization. */
1344 int max_regno
= max_reg_num ();
1346 if (! flag_lra_remat
)
1348 lra_rematerialization_iter
++;
1349 if (lra_rematerialization_iter
> LRA_MAX_REMATERIALIZATION_PASSES
)
1351 if (lra_dump_file
!= NULL
)
1352 fprintf (lra_dump_file
,
1353 "\n******** Rematerialization #%d: ********\n\n",
1354 lra_rematerialization_iter
);
1355 timevar_push (TV_LRA_REMAT
);
1356 insn_to_cand
= XCNEWVEC (cand_t
, get_max_uid ());
1357 insn_to_cand_activation
= XCNEWVEC (cand_t
, get_max_uid ());
1358 regno_cands
= XCNEWVEC (cand_t
, max_regno
);
1359 all_cands
.create (8000);
1360 call_used_regs_arr_len
= 0;
1361 for (int i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1362 if (call_used_regs
[i
])
1363 call_used_regs_arr
[call_used_regs_arr_len
++] = i
;
1364 initiate_cand_table ();
1365 create_remat_bb_data ();
1366 bitmap_initialize (&temp_bitmap
, ®_obstack
);
1367 bitmap_initialize (&subreg_regs
, ®_obstack
);
1368 calculate_local_reg_remat_bb_data ();
1370 calculate_livein_cands ();
1371 calculate_gen_cands ();
1372 bitmap_initialize (&all_blocks
, ®_obstack
);
1373 FOR_ALL_BB_FN (bb
, cfun
)
1374 bitmap_set_bit (&all_blocks
, bb
->index
);
1375 calculate_global_remat_bb_data ();
1376 dump_candidates_and_remat_bb_data ();
1377 result
= do_remat ();
1378 all_cands
.release ();
1379 bitmap_clear (&temp_bitmap
);
1380 bitmap_clear (&subreg_regs
);
1381 finish_remat_bb_data ();
1382 finish_cand_table ();
1383 bitmap_clear (&all_blocks
);
1385 free (insn_to_cand
);
1386 free (insn_to_cand_activation
);
1387 timevar_pop (TV_LRA_REMAT
);