* combine.c (combine_validate_cost): Do not count the cost of a
[official-gcc.git] / gcc / lra-remat.c
blob768c1fb2323363ebb98992f1e693b98d89dad8fa
1 /* Rematerialize pseudos values.
2 Copyright (C) 2014-2015 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
10 version.
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
15 for more details.
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
36 sparse.
38 The rematerialization sub-pass could be improved further in the
39 following ways:
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
47 scope.
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
52 be very rare. */
55 #include "config.h"
56 #include "system.h"
57 #include "coretypes.h"
58 #include "tm.h"
59 #include "hard-reg-set.h"
60 #include "rtl.h"
61 #include "rtl-error.h"
62 #include "tm_p.h"
63 #include "target.h"
64 #include "insn-config.h"
65 #include "recog.h"
66 #include "output.h"
67 #include "regs.h"
68 #include "hashtab.h"
69 #include "hash-set.h"
70 #include "vec.h"
71 #include "machmode.h"
72 #include "input.h"
73 #include "function.h"
74 #include "expr.h"
75 #include "predict.h"
76 #include "dominance.h"
77 #include "cfg.h"
78 #include "basic-block.h"
79 #include "except.h"
80 #include "df.h"
81 #include "ira.h"
82 #include "sparseset.h"
83 #include "params.h"
84 #include "df.h"
85 #include "lra-int.h"
87 /* Number of candidates for rematerialization. */
88 static unsigned int cands_num;
90 /* The following is used for representation of call_used_reg_set in
91 form array whose elements are hard register numbers with nonzero bit
92 in CALL_USED_REG_SET. */
93 static int call_used_regs_arr_len;
94 static int call_used_regs_arr[FIRST_PSEUDO_REGISTER];
96 /* Bitmap used for different calculations. */
97 static bitmap_head temp_bitmap;
99 typedef struct cand *cand_t;
100 typedef const struct cand *const_cand_t;
102 /* Insn candidates for rematerialization. The candidate insn should
103 have the following properies:
104 o no any memory (as access to memory is non-profitable)
105 o no INOUT regs (it means no non-paradoxical subreg of output reg)
106 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
107 o all other pseudos are with assigned hard regs. */
108 struct cand
110 /* Index of the candidates in all_cands. */
111 int index;
112 /* The candidate insn. */
113 rtx_insn *insn;
114 /* Insn pseudo regno for rematerialization. */
115 int regno;
116 /* Non-negative if a reload pseudo is in the insn instead of the
117 pseudo for rematerialization. */
118 int reload_regno;
119 /* Number of the operand containing the regno or its reload
120 regno. */
121 int nop;
122 /* Next candidate for the same regno. */
123 cand_t next_regno_cand;
126 /* Vector containing all candidates. */
127 static vec<cand_t> all_cands;
128 /* Map: insn -> candidate representing it. It is null if the insn can
129 not be used for rematerialization. */
130 static cand_t *insn_to_cand;
132 /* Map regno -> candidates can be used for the regno
133 rematerialization. */
134 static cand_t *regno_cands;
136 /* Data about basic blocks used for the rematerialization
137 sub-pass. */
138 struct remat_bb_data
140 /* Basic block about which the below data are. */
141 basic_block bb;
142 /* Registers changed in the basic block: */
143 bitmap_head changed_regs;
144 /* Registers becoming dead in the BB. */
145 bitmap_head dead_regs;
146 /* Cands present in the BB whose in/out regs are not changed after
147 the cands occurence and are not dead (except the reload
148 regno). */
149 bitmap_head gen_cands;
150 bitmap_head livein_cands; /* cands whose inputs live at the BB start. */
151 bitmap_head pavin_cands; /* cands partially available at BB entry. */
152 bitmap_head pavout_cands; /* cands partially available at BB exit. */
153 bitmap_head avin_cands; /* cands available at the entry of the BB. */
154 bitmap_head avout_cands; /* cands available at the exit of the BB. */
157 /* Array for all BB data. Indexed by the corresponding BB index. */
158 typedef struct remat_bb_data *remat_bb_data_t;
160 /* Basic blocks for data flow problems -- all bocks except the special
161 ones. */
162 static bitmap_head all_blocks;
164 /* All basic block data are referred through the following array. */
165 static remat_bb_data_t remat_bb_data;
167 /* Two small functions for access to the bb data. */
168 static inline remat_bb_data_t
169 get_remat_bb_data (basic_block bb)
171 return &remat_bb_data[(bb)->index];
174 static inline remat_bb_data_t
175 get_remat_bb_data_by_index (int index)
177 return &remat_bb_data[index];
182 /* Recursive hash function for RTL X. */
183 static hashval_t
184 rtx_hash (rtx x)
186 int i, j;
187 enum rtx_code code;
188 const char *fmt;
189 hashval_t val = 0;
191 if (x == 0)
192 return val;
194 code = GET_CODE (x);
195 val += (int) code + 4095;
197 /* Some RTL can be compared nonrecursively. */
198 switch (code)
200 case REG:
201 return val + REGNO (x);
203 case LABEL_REF:
204 return iterative_hash_object (XEXP (x, 0), val);
206 case SYMBOL_REF:
207 return iterative_hash_object (XSTR (x, 0), val);
209 case SCRATCH:
210 case CONST_DOUBLE:
211 case CONST_INT:
212 case CONST_VECTOR:
213 return val;
215 default:
216 break;
219 /* Hash the elements. */
220 fmt = GET_RTX_FORMAT (code);
221 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
223 switch (fmt[i])
225 case 'w':
226 val += XWINT (x, i);
227 break;
229 case 'n':
230 case 'i':
231 val += XINT (x, i);
232 break;
234 case 'V':
235 case 'E':
236 val += XVECLEN (x, i);
238 for (j = 0; j < XVECLEN (x, i); j++)
239 val += rtx_hash (XVECEXP (x, i, j));
240 break;
242 case 'e':
243 val += rtx_hash (XEXP (x, i));
244 break;
246 case 'S':
247 case 's':
248 val += htab_hash_string (XSTR (x, i));
249 break;
251 case 'u':
252 case '0':
253 case 't':
254 break;
256 /* It is believed that rtx's at this level will never
257 contain anything but integers and other rtx's, except for
258 within LABEL_REFs and SYMBOL_REFs. */
259 default:
260 abort ();
263 return val;
268 /* Hash table for the candidates. Different insns (e.g. structurally
269 the same insns or even insns with different unused output regs) can
270 be represented by the same candidate in the table. */
271 static htab_t cand_table;
273 /* Hash function for candidate CAND. */
274 static hashval_t
275 cand_hash (const void *cand)
277 const_cand_t c = (const_cand_t) cand;
278 lra_insn_recog_data_t id = lra_get_insn_recog_data (c->insn);
279 struct lra_static_insn_data *static_id = id->insn_static_data;
280 int nops = static_id->n_operands;
281 hashval_t hash = 0;
283 for (int i = 0; i < nops; i++)
284 if (i == c->nop)
285 hash = iterative_hash_object (c->regno, hash);
286 else if (static_id->operand[i].type == OP_IN)
287 hash = iterative_hash_object (*id->operand_loc[i], hash);
288 return hash;
291 /* Equal function for candidates CAND1 and CAND2. They are equal if
292 the corresponding candidate insns have the same code, the same
293 regno for rematerialization, the same input operands. */
294 static int
295 cand_eq_p (const void *cand1, const void *cand2)
297 const_cand_t c1 = (const_cand_t) cand1;
298 const_cand_t c2 = (const_cand_t) cand2;
299 lra_insn_recog_data_t id1 = lra_get_insn_recog_data (c1->insn);
300 lra_insn_recog_data_t id2 = lra_get_insn_recog_data (c2->insn);
301 struct lra_static_insn_data *static_id1 = id1->insn_static_data;
302 int nops = static_id1->n_operands;
304 if (c1->regno != c2->regno
305 || INSN_CODE (c1->insn) < 0
306 || INSN_CODE (c1->insn) != INSN_CODE (c2->insn))
307 return false;
308 gcc_assert (c1->nop == c2->nop);
309 for (int i = 0; i < nops; i++)
310 if (i != c1->nop && static_id1->operand[i].type == OP_IN
311 && *id1->operand_loc[i] != *id2->operand_loc[i])
312 return false;
313 return true;
316 /* Insert candidate CAND into the table if it is not there yet.
317 Return candidate which is in the table. */
318 static cand_t
319 insert_cand (cand_t cand)
321 void **entry_ptr;
323 entry_ptr = htab_find_slot (cand_table, cand, INSERT);
324 if (*entry_ptr == NULL)
325 *entry_ptr = (void *) cand;
326 return (cand_t) *entry_ptr;
329 /* Free candidate CAND memory. */
330 static void
331 free_cand (void *cand)
333 free (cand);
336 /* Initiate the candidate table. */
337 static void
338 initiate_cand_table (void)
340 cand_table = htab_create (8000, cand_hash, cand_eq_p,
341 (htab_del) free_cand);
344 /* Finish the candidate table. */
345 static void
346 finish_cand_table (void)
348 htab_delete (cand_table);
353 /* Return true if X contains memory or some UNSPEC. We can not just
354 check insn operands as memory or unspec might be not an operand
355 itself but contain an operand. Insn with memory access is not
356 profitable for rematerialization. Rematerialization of UNSPEC
357 might result in wrong code generation as the UNPEC effect is
358 unknown (e.g. generating a label). */
359 static bool
360 bad_for_rematerialization_p (rtx x)
362 int i, j;
363 const char *fmt;
364 enum rtx_code code;
366 if (MEM_P (x) || GET_CODE (x) == UNSPEC || GET_CODE (x) == UNSPEC_VOLATILE)
367 return true;
368 code = GET_CODE (x);
369 fmt = GET_RTX_FORMAT (code);
370 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
372 if (fmt[i] == 'e')
374 if (bad_for_rematerialization_p (XEXP (x, i)))
375 return true;
377 else if (fmt[i] == 'E')
379 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
380 if (bad_for_rematerialization_p (XVECEXP (x, i, j)))
381 return true;
384 return false;
387 /* If INSN can not be used for rematerialization, return negative
388 value. If INSN can be considered as a candidate for
389 rematerialization, return value which is the operand number of the
390 pseudo for which the insn can be used for rematerialization. Here
391 we consider the insns without any memory, spilled pseudo (except
392 for the rematerialization pseudo), or dying or unused regs. */
393 static int
394 operand_to_remat (rtx_insn *insn)
396 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
397 struct lra_static_insn_data *static_id = id->insn_static_data;
398 struct lra_insn_reg *reg, *found_reg = NULL;
400 /* First find a pseudo which can be rematerialized. */
401 for (reg = id->regs; reg != NULL; reg = reg->next)
402 /* True FRAME_POINTER_NEEDED might be because we can not follow
403 changing sp offsets, e.g. alloca is used. If the insn contains
404 stack pointer in such case, we can not rematerialize it as we
405 can not know sp offset at a rematerialization place. */
406 if (reg->regno == STACK_POINTER_REGNUM && frame_pointer_needed)
407 return -1;
408 else if (reg->type == OP_OUT && ! reg->subreg_p
409 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
411 /* We permits only one spilled reg. */
412 if (found_reg != NULL)
413 return -1;
414 found_reg = reg;
416 if (found_reg == NULL)
417 return -1;
418 if (found_reg->regno < FIRST_PSEUDO_REGISTER)
419 return -1;
420 if (bad_for_rematerialization_p (PATTERN (insn)))
421 return -1;
422 /* Check the other regs are not spilled. */
423 for (reg = id->regs; reg != NULL; reg = reg->next)
424 if (found_reg == reg)
425 continue;
426 else if (reg->type == OP_INOUT)
427 return -1;
428 else if (reg->regno >= FIRST_PSEUDO_REGISTER
429 && reg_renumber[reg->regno] < 0)
430 /* Another spilled reg. */
431 return -1;
432 else if (reg->type == OP_IN)
434 if (find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
435 /* We don't want to make live ranges longer. */
436 return -1;
437 /* Check that there is no output reg as the input one. */
438 for (struct lra_insn_reg *reg2 = id->regs;
439 reg2 != NULL;
440 reg2 = reg2->next)
441 if (reg2->type == OP_OUT && reg->regno == reg2->regno)
442 return -1;
444 /* Find the rematerialization operand. */
445 int nop = static_id->n_operands;
446 for (int i = 0; i < nop; i++)
447 if (REG_P (*id->operand_loc[i])
448 && (int) REGNO (*id->operand_loc[i]) == found_reg->regno)
449 return i;
450 return -1;
453 /* Create candidate for INSN with rematerialization operand NOP and
454 REGNO. Insert the candidate into the table and set up the
455 corresponding INSN_TO_CAND element. */
456 static void
457 create_cand (rtx_insn *insn, int nop, int regno)
459 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
460 rtx reg = *id->operand_loc[nop];
461 gcc_assert (REG_P (reg));
462 int op_regno = REGNO (reg);
463 gcc_assert (op_regno >= FIRST_PSEUDO_REGISTER);
464 cand_t cand = XNEW (struct cand);
465 cand->insn = insn;
466 cand->nop = nop;
467 cand->regno = regno;
468 cand->reload_regno = op_regno == regno ? -1 : op_regno;
469 gcc_assert (cand->regno >= 0);
470 cand_t cand_in_table = insert_cand (cand);
471 insn_to_cand[INSN_UID (insn)] = cand_in_table;
472 if (cand != cand_in_table)
473 free (cand);
474 else
476 /* A new cand. */
477 cand->index = all_cands.length ();
478 all_cands.safe_push (cand);
479 cand->next_regno_cand = regno_cands[cand->regno];
480 regno_cands[cand->regno] = cand;
484 /* Create rematerialization candidates (inserting them into the
485 table). */
486 static void
487 create_cands (void)
489 rtx_insn *insn;
490 struct potential_cand
492 rtx_insn *insn;
493 int nop;
495 struct potential_cand *regno_potential_cand;
497 /* Create candidates. */
498 regno_potential_cand = XCNEWVEC (struct potential_cand, max_reg_num ());
499 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
500 if (INSN_P (insn))
502 rtx set;
503 int src_regno, dst_regno;
504 rtx_insn *insn2;
505 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
506 int nop = operand_to_remat (insn);
507 int regno = -1;
509 if ((set = single_set (insn)) != NULL
510 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
511 && ((src_regno = REGNO (SET_SRC (set)))
512 >= lra_constraint_new_regno_start)
513 && (dst_regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
514 && reg_renumber[dst_regno] < 0
515 && (insn2 = regno_potential_cand[src_regno].insn) != NULL
516 && BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn))
517 /* It is an output reload insn after insn can be
518 rematerialized (potential candidate). */
519 create_cand (insn2, regno_potential_cand[src_regno].nop, dst_regno);
520 if (nop < 0)
521 goto fail;
522 gcc_assert (REG_P (*id->operand_loc[nop]));
523 regno = REGNO (*id->operand_loc[nop]);
524 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
525 if (reg_renumber[regno] < 0)
526 create_cand (insn, nop, regno);
527 else
529 regno_potential_cand[regno].insn = insn;
530 regno_potential_cand[regno].nop = nop;
531 goto fail;
533 regno = -1;
534 fail:
535 for (struct lra_insn_reg *reg = id->regs; reg != NULL; reg = reg->next)
536 if (reg->type != OP_IN && reg->regno != regno
537 && reg->regno >= FIRST_PSEUDO_REGISTER)
538 regno_potential_cand[reg->regno].insn = NULL;
540 cands_num = all_cands.length ();
541 free (regno_potential_cand);
546 /* Create and initialize BB data. */
547 static void
548 create_remat_bb_data (void)
550 basic_block bb;
551 remat_bb_data_t bb_info;
553 remat_bb_data = XNEWVEC (struct remat_bb_data,
554 last_basic_block_for_fn (cfun));
555 FOR_ALL_BB_FN (bb, cfun)
557 #ifdef ENABLE_CHECKING
558 if (bb->index < 0 || bb->index >= last_basic_block_for_fn (cfun))
559 abort ();
560 #endif
561 bb_info = get_remat_bb_data (bb);
562 bb_info->bb = bb;
563 bitmap_initialize (&bb_info->changed_regs, &reg_obstack);
564 bitmap_initialize (&bb_info->dead_regs, &reg_obstack);
565 bitmap_initialize (&bb_info->gen_cands, &reg_obstack);
566 bitmap_initialize (&bb_info->livein_cands, &reg_obstack);
567 bitmap_initialize (&bb_info->pavin_cands, &reg_obstack);
568 bitmap_initialize (&bb_info->pavout_cands, &reg_obstack);
569 bitmap_initialize (&bb_info->avin_cands, &reg_obstack);
570 bitmap_initialize (&bb_info->avout_cands, &reg_obstack);
574 /* Dump all candidates to DUMP_FILE. */
575 static void
576 dump_cands (FILE *dump_file)
578 int i;
579 cand_t cand;
581 fprintf (dump_file, "\nCands:\n");
582 for (i = 0; i < (int) cands_num; i++)
584 cand = all_cands[i];
585 fprintf (dump_file, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
586 i, cand->nop, cand->regno, cand->reload_regno);
587 print_inline_rtx (dump_file, cand->insn, 6);
588 fprintf (dump_file, "\n");
592 /* Dump all candidates and BB data. */
593 static void
594 dump_candidates_and_remat_bb_data (void)
596 basic_block bb;
598 if (lra_dump_file == NULL)
599 return;
600 dump_cands (lra_dump_file);
601 FOR_EACH_BB_FN (bb, cfun)
603 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
604 /* Livein */
605 fprintf (lra_dump_file, " register live in:");
606 dump_regset (df_get_live_in (bb), lra_dump_file);
607 putc ('\n', lra_dump_file);
608 /* Liveout */
609 fprintf (lra_dump_file, " register live out:");
610 dump_regset (df_get_live_out (bb), lra_dump_file);
611 putc ('\n', lra_dump_file);
612 /* Changed/dead regs: */
613 fprintf (lra_dump_file, " changed regs:");
614 dump_regset (&get_remat_bb_data (bb)->changed_regs, lra_dump_file);
615 putc ('\n', lra_dump_file);
616 fprintf (lra_dump_file, " dead regs:");
617 dump_regset (&get_remat_bb_data (bb)->dead_regs, lra_dump_file);
618 putc ('\n', lra_dump_file);
619 lra_dump_bitmap_with_title ("cands generated in BB",
620 &get_remat_bb_data (bb)->gen_cands, bb->index);
621 lra_dump_bitmap_with_title ("livein cands in BB",
622 &get_remat_bb_data (bb)->livein_cands, bb->index);
623 lra_dump_bitmap_with_title ("pavin cands in BB",
624 &get_remat_bb_data (bb)->pavin_cands, bb->index);
625 lra_dump_bitmap_with_title ("pavout cands in BB",
626 &get_remat_bb_data (bb)->pavout_cands, bb->index);
627 lra_dump_bitmap_with_title ("avin cands in BB",
628 &get_remat_bb_data (bb)->avin_cands, bb->index);
629 lra_dump_bitmap_with_title ("avout cands in BB",
630 &get_remat_bb_data (bb)->avout_cands, bb->index);
634 /* Free all BB data. */
635 static void
636 finish_remat_bb_data (void)
638 basic_block bb;
640 FOR_EACH_BB_FN (bb, cfun)
642 bitmap_clear (&get_remat_bb_data (bb)->avout_cands);
643 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
644 bitmap_clear (&get_remat_bb_data (bb)->pavout_cands);
645 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
646 bitmap_clear (&get_remat_bb_data (bb)->livein_cands);
647 bitmap_clear (&get_remat_bb_data (bb)->gen_cands);
648 bitmap_clear (&get_remat_bb_data (bb)->dead_regs);
649 bitmap_clear (&get_remat_bb_data (bb)->changed_regs);
651 free (remat_bb_data);
656 /* Update changed_regs and dead_regs of BB from INSN. */
657 static void
658 set_bb_regs (basic_block bb, rtx_insn *insn)
660 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
661 struct lra_insn_reg *reg;
663 for (reg = id->regs; reg != NULL; reg = reg->next)
664 if (reg->type != OP_IN)
665 bitmap_set_bit (&get_remat_bb_data (bb)->changed_regs, reg->regno);
666 else
668 if (find_regno_note (insn, REG_DEAD, (unsigned) reg->regno) != NULL)
669 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs, reg->regno);
671 if (CALL_P (insn))
672 for (int i = 0; i < call_used_regs_arr_len; i++)
673 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs,
674 call_used_regs_arr[i]);
677 /* Calculate changed_regs and dead_regs for each BB. */
678 static void
679 calculate_local_reg_remat_bb_data (void)
681 basic_block bb;
682 rtx_insn *insn;
684 FOR_EACH_BB_FN (bb, cfun)
685 FOR_BB_INSNS (bb, insn)
686 if (INSN_P (insn))
687 set_bb_regs (bb, insn);
692 /* Return true if REGNO is an input operand of INSN. */
693 static bool
694 input_regno_present_p (rtx_insn *insn, int regno)
696 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
697 struct lra_insn_reg *reg;
699 for (reg = id->regs; reg != NULL; reg = reg->next)
700 if (reg->type == OP_IN && reg->regno == regno)
701 return true;
702 return false;
705 /* Return true if a call used register is an input operand of INSN. */
706 static bool
707 call_used_input_regno_present_p (rtx_insn *insn)
709 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
710 struct lra_insn_reg *reg;
712 for (reg = id->regs; reg != NULL; reg = reg->next)
713 if (reg->type == OP_IN && reg->regno <= FIRST_PSEUDO_REGISTER
714 && TEST_HARD_REG_BIT (call_used_reg_set, reg->regno))
715 return true;
716 return false;
719 /* Calculate livein_cands for each BB. */
720 static void
721 calculate_livein_cands (void)
723 basic_block bb;
725 FOR_EACH_BB_FN (bb, cfun)
727 bitmap livein_regs = df_get_live_in (bb);
728 bitmap livein_cands = &get_remat_bb_data (bb)->livein_cands;
729 for (unsigned int i = 0; i < cands_num; i++)
731 cand_t cand = all_cands[i];
732 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
733 struct lra_insn_reg *reg;
735 for (reg = id->regs; reg != NULL; reg = reg->next)
736 if (reg->type == OP_IN && ! bitmap_bit_p (livein_regs, reg->regno))
737 break;
738 if (reg == NULL)
739 bitmap_set_bit (livein_cands, i);
744 /* Calculate gen_cands for each BB. */
745 static void
746 calculate_gen_cands (void)
748 basic_block bb;
749 bitmap gen_cands;
750 bitmap_head gen_insns;
751 rtx_insn *insn;
753 bitmap_initialize (&gen_insns, &reg_obstack);
754 FOR_EACH_BB_FN (bb, cfun)
756 gen_cands = &get_remat_bb_data (bb)->gen_cands;
757 bitmap_clear (&gen_insns);
758 FOR_BB_INSNS (bb, insn)
759 if (INSN_P (insn))
761 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
762 struct lra_insn_reg *reg;
763 unsigned int uid;
764 bitmap_iterator bi;
765 cand_t cand;
766 rtx set;
767 int src_regno = -1, dst_regno = -1;
769 if ((set = single_set (insn)) != NULL
770 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
772 src_regno = REGNO (SET_SRC (set));
773 dst_regno = REGNO (SET_DEST (set));
776 /* Update gen_cands: */
777 bitmap_clear (&temp_bitmap);
778 for (reg = id->regs; reg != NULL; reg = reg->next)
779 if (reg->type != OP_IN
780 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
781 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
783 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
785 cand = insn_to_cand[INSN_UID (insn2)];
786 gcc_assert (cand != NULL);
787 /* Ignore the reload insn. */
788 if (src_regno == cand->reload_regno
789 && dst_regno == cand->regno)
790 continue;
791 if (cand->regno == reg->regno
792 || input_regno_present_p (insn2, reg->regno))
794 bitmap_clear_bit (gen_cands, cand->index);
795 bitmap_set_bit (&temp_bitmap, uid);
799 if (CALL_P (insn))
800 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
802 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
804 cand = insn_to_cand[INSN_UID (insn2)];
805 gcc_assert (cand != NULL);
806 if (call_used_input_regno_present_p (insn2))
808 bitmap_clear_bit (gen_cands, cand->index);
809 bitmap_set_bit (&temp_bitmap, uid);
812 bitmap_and_compl_into (&gen_insns, &temp_bitmap);
814 cand = insn_to_cand[INSN_UID (insn)];
815 if (cand != NULL)
817 bitmap_set_bit (gen_cands, cand->index);
818 bitmap_set_bit (&gen_insns, INSN_UID (insn));
822 bitmap_clear (&gen_insns);
827 /* The common transfer function used by the DF equation solver to
828 propagate (partial) availability info BB_IN to BB_OUT through block
829 with BB_INDEX according to the following equation:
831 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
833 static bool
834 cand_trans_fun (int bb_index, bitmap bb_in, bitmap bb_out)
836 remat_bb_data_t bb_info;
837 bitmap bb_livein, bb_changed_regs, bb_dead_regs;
838 unsigned int cid;
839 bitmap_iterator bi;
841 bb_info = get_remat_bb_data_by_index (bb_index);
842 bb_livein = &bb_info->livein_cands;
843 bb_changed_regs = &bb_info->changed_regs;
844 bb_dead_regs = &bb_info->dead_regs;
845 /* Calculate killed avin cands -- cands whose regs are changed or
846 becoming dead in the BB. We calculate it here as we hope that
847 repeated calculations are compensated by smaller size of BB_IN in
848 comparison with all candidates number. */
849 bitmap_clear (&temp_bitmap);
850 EXECUTE_IF_SET_IN_BITMAP (bb_in, 0, cid, bi)
852 cand_t cand = all_cands[cid];
853 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
854 struct lra_insn_reg *reg;
856 if (! bitmap_bit_p (bb_livein, cid))
858 bitmap_set_bit (&temp_bitmap, cid);
859 continue;
861 for (reg = id->regs; reg != NULL; reg = reg->next)
862 /* Ignore all outputs which are not the regno for
863 rematerialization. */
864 if (reg->type == OP_OUT && reg->regno != cand->regno)
865 continue;
866 else if (bitmap_bit_p (bb_changed_regs, reg->regno)
867 || bitmap_bit_p (bb_dead_regs, reg->regno))
869 bitmap_set_bit (&temp_bitmap, cid);
870 break;
872 /* Check regno for rematerialization. */
873 if (bitmap_bit_p (bb_changed_regs, cand->regno)
874 || bitmap_bit_p (bb_dead_regs, cand->regno))
875 bitmap_set_bit (&temp_bitmap, cid);
877 return bitmap_ior_and_compl (bb_out,
878 &bb_info->gen_cands, bb_in, &temp_bitmap);
883 /* The transfer function used by the DF equation solver to propagate
884 partial candidate availability info through block with BB_INDEX
885 according to the following equation:
887 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
889 static bool
890 cand_pav_trans_fun (int bb_index)
892 remat_bb_data_t bb_info;
894 bb_info = get_remat_bb_data_by_index (bb_index);
895 return cand_trans_fun (bb_index, &bb_info->pavin_cands,
896 &bb_info->pavout_cands);
899 /* The confluence function used by the DF equation solver to set up
900 cand_pav info for a block BB without predecessor. */
901 static void
902 cand_pav_con_fun_0 (basic_block bb)
904 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
907 /* The confluence function used by the DF equation solver to propagate
908 partial candidate availability info from predecessor to successor
909 on edge E (pred->bb) according to the following equation:
911 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
913 static bool
914 cand_pav_con_fun_n (edge e)
916 basic_block pred = e->src;
917 basic_block bb = e->dest;
918 remat_bb_data_t bb_info;
919 bitmap bb_pavin, pred_pavout;
921 bb_info = get_remat_bb_data (bb);
922 bb_pavin = &bb_info->pavin_cands;
923 pred_pavout = &get_remat_bb_data (pred)->pavout_cands;
924 return bitmap_ior_into (bb_pavin, pred_pavout);
929 /* The transfer function used by the DF equation solver to propagate
930 candidate availability info through block with BB_INDEX according
931 to the following equation:
933 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
935 static bool
936 cand_av_trans_fun (int bb_index)
938 remat_bb_data_t bb_info;
940 bb_info = get_remat_bb_data_by_index (bb_index);
941 return cand_trans_fun (bb_index, &bb_info->avin_cands,
942 &bb_info->avout_cands);
945 /* The confluence function used by the DF equation solver to set up
946 cand_av info for a block BB without predecessor. */
947 static void
948 cand_av_con_fun_0 (basic_block bb)
950 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
953 /* The confluence function used by the DF equation solver to propagate
954 cand_av info from predecessor to successor on edge E (pred->bb)
955 according to the following equation:
957 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
959 static bool
960 cand_av_con_fun_n (edge e)
962 basic_block pred = e->src;
963 basic_block bb = e->dest;
964 remat_bb_data_t bb_info;
965 bitmap bb_avin, pred_avout;
967 bb_info = get_remat_bb_data (bb);
968 bb_avin = &bb_info->avin_cands;
969 pred_avout = &get_remat_bb_data (pred)->avout_cands;
970 return bitmap_and_into (bb_avin, pred_avout);
973 /* Calculate available candidates for each BB. */
974 static void
975 calculate_global_remat_bb_data (void)
977 basic_block bb;
979 df_simple_dataflow
980 (DF_FORWARD, NULL, cand_pav_con_fun_0, cand_pav_con_fun_n,
981 cand_pav_trans_fun, &all_blocks,
982 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
983 /* Initialize avin by pavin. */
984 FOR_EACH_BB_FN (bb, cfun)
985 bitmap_copy (&get_remat_bb_data (bb)->avin_cands,
986 &get_remat_bb_data (bb)->pavin_cands);
987 df_simple_dataflow
988 (DF_FORWARD, NULL, cand_av_con_fun_0, cand_av_con_fun_n,
989 cand_av_trans_fun, &all_blocks,
990 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
995 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
996 static void
997 change_sp_offset (rtx_insn *insns, HOST_WIDE_INT sp_offset)
999 for (rtx_insn *insn = insns; insn != NULL; insn = NEXT_INSN (insn))
1000 eliminate_regs_in_insn (insn, false, false, sp_offset);
1003 /* Return start hard register of REG (can be a hard or a pseudo reg)
1004 or -1 (if it is a spilled pseudo). Return number of hard registers
1005 occupied by REG through parameter NREGS if the start hard reg is
1006 not negative. */
1007 static int
1008 get_hard_regs (struct lra_insn_reg *reg, int &nregs)
1010 int regno = reg->regno;
1011 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
1013 if (hard_regno >= 0)
1014 nregs = hard_regno_nregs[hard_regno][reg->biggest_mode];
1015 return hard_regno;
1018 /* Insert rematerialization insns using the data-flow data calculated
1019 earlier. */
1020 static bool
1021 do_remat (void)
1023 rtx_insn *insn;
1024 basic_block bb;
1025 bitmap_head avail_cands;
1026 bool changed_p = false;
1027 /* Living hard regs and hard registers of living pseudos. */
1028 HARD_REG_SET live_hard_regs;
1030 bitmap_initialize (&avail_cands, &reg_obstack);
1031 FOR_EACH_BB_FN (bb, cfun)
1033 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (bb));
1034 bitmap_and (&avail_cands, &get_remat_bb_data (bb)->avin_cands,
1035 &get_remat_bb_data (bb)->livein_cands);
1036 FOR_BB_INSNS (bb, insn)
1038 if (!NONDEBUG_INSN_P (insn))
1039 continue;
1041 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1042 struct lra_static_insn_data *static_id = id->insn_static_data;
1043 struct lra_insn_reg *reg;
1044 cand_t cand;
1045 unsigned int cid;
1046 bitmap_iterator bi;
1047 rtx set;
1048 int src_regno = -1, dst_regno = -1;
1050 if ((set = single_set (insn)) != NULL
1051 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
1053 src_regno = REGNO (SET_SRC (set));
1054 dst_regno = REGNO (SET_DEST (set));
1057 cand = NULL;
1058 /* Check possibility of rematerialization (hard reg or
1059 unpsilled pseudo <- spilled pseudo): */
1060 if (dst_regno >= 0 && src_regno >= FIRST_PSEUDO_REGISTER
1061 && reg_renumber[src_regno] < 0
1062 && (dst_regno < FIRST_PSEUDO_REGISTER
1063 || reg_renumber[dst_regno] >= 0))
1065 for (cand = regno_cands[src_regno];
1066 cand != NULL;
1067 cand = cand->next_regno_cand)
1068 if (bitmap_bit_p (&avail_cands, cand->index))
1069 break;
1071 int i, hard_regno, nregs;
1072 rtx_insn *remat_insn = NULL;
1073 HOST_WIDE_INT cand_sp_offset = 0;
1074 if (cand != NULL)
1076 lra_insn_recog_data_t cand_id
1077 = lra_get_insn_recog_data (cand->insn);
1078 struct lra_static_insn_data *static_cand_id
1079 = cand_id->insn_static_data;
1080 rtx saved_op = *cand_id->operand_loc[cand->nop];
1082 /* Check clobbers do not kill something living. */
1083 gcc_assert (REG_P (saved_op));
1084 int ignore_regno = REGNO (saved_op);
1086 for (reg = cand_id->regs; reg != NULL; reg = reg->next)
1087 if (reg->type != OP_IN && reg->regno != ignore_regno)
1089 hard_regno = get_hard_regs (reg, nregs);
1090 gcc_assert (hard_regno >= 0);
1091 for (i = 0; i < nregs; i++)
1092 if (TEST_HARD_REG_BIT (live_hard_regs, hard_regno + i))
1093 break;
1094 if (i < nregs)
1095 break;
1098 if (reg == NULL)
1100 for (reg = static_cand_id->hard_regs;
1101 reg != NULL;
1102 reg = reg->next)
1103 if (reg->type != OP_IN
1104 && TEST_HARD_REG_BIT (live_hard_regs, reg->regno))
1105 break;
1108 if (reg == NULL)
1110 *cand_id->operand_loc[cand->nop] = SET_DEST (set);
1111 lra_update_insn_regno_info (cand->insn);
1112 bool ok_p = lra_constrain_insn (cand->insn);
1113 if (ok_p)
1115 rtx remat_pat = copy_insn (PATTERN (cand->insn));
1117 start_sequence ();
1118 emit_insn (remat_pat);
1119 remat_insn = get_insns ();
1120 end_sequence ();
1121 if (recog_memoized (remat_insn) < 0)
1122 remat_insn = NULL;
1123 cand_sp_offset = cand_id->sp_offset;
1125 *cand_id->operand_loc[cand->nop] = saved_op;
1126 lra_update_insn_regno_info (cand->insn);
1130 bitmap_clear (&temp_bitmap);
1131 /* Update avail_cands (see analogous code for
1132 calculate_gen_cands). */
1133 for (reg = id->regs; reg != NULL; reg = reg->next)
1134 if (reg->type != OP_IN
1135 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1136 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1138 cand = all_cands[cid];
1140 /* Ignore the reload insn. */
1141 if (src_regno == cand->reload_regno
1142 && dst_regno == cand->regno)
1143 continue;
1144 if (cand->regno == reg->regno
1145 || input_regno_present_p (cand->insn, reg->regno))
1146 bitmap_set_bit (&temp_bitmap, cand->index);
1149 if (CALL_P (insn))
1150 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1152 cand = all_cands[cid];
1154 if (call_used_input_regno_present_p (cand->insn))
1155 bitmap_set_bit (&temp_bitmap, cand->index);
1158 bitmap_and_compl_into (&avail_cands, &temp_bitmap);
1159 if ((cand = insn_to_cand[INSN_UID (insn)]) != NULL)
1160 bitmap_set_bit (&avail_cands, cand->index);
1162 if (remat_insn != NULL)
1164 HOST_WIDE_INT sp_offset_change = cand_sp_offset - id->sp_offset;
1165 if (sp_offset_change != 0)
1166 change_sp_offset (remat_insn, sp_offset_change);
1167 lra_process_new_insns (insn, remat_insn, NULL,
1168 "Inserting rematerialization insn");
1169 lra_set_insn_deleted (insn);
1170 changed_p = true;
1171 continue;
1174 /* Update live hard regs: */
1175 for (reg = id->regs; reg != NULL; reg = reg->next)
1176 if (reg->type == OP_IN
1177 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1179 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1180 continue;
1181 for (i = 0; i < nregs; i++)
1182 CLEAR_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1184 else if (reg->type != OP_IN
1185 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1187 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1188 continue;
1189 for (i = 0; i < nregs; i++)
1190 SET_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1192 /* Process also hard regs (e.g. CC register) which are part
1193 of insn definition. */
1194 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1195 if (reg->type == OP_IN
1196 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1197 CLEAR_HARD_REG_BIT (live_hard_regs, reg->regno);
1198 else if (reg->type != OP_IN
1199 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1200 SET_HARD_REG_BIT (live_hard_regs, reg->regno);
1203 bitmap_clear (&avail_cands);
1204 return changed_p;
1209 /* Entry point of the rematerialization sub-pass. Return true if we
1210 did any rematerialization. */
1211 bool
1212 lra_remat (void)
1214 basic_block bb;
1215 bool result;
1216 int max_regno = max_reg_num ();
1218 if (! flag_lra_remat)
1219 return false;
1220 timevar_push (TV_LRA_REMAT);
1221 insn_to_cand = XCNEWVEC (cand_t, get_max_uid ());
1222 regno_cands = XCNEWVEC (cand_t, max_regno);
1223 all_cands.create (8000);
1224 call_used_regs_arr_len = 0;
1225 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1226 if (call_used_regs[i])
1227 call_used_regs_arr[call_used_regs_arr_len++] = i;
1228 initiate_cand_table ();
1229 create_cands ();
1230 create_remat_bb_data ();
1231 bitmap_initialize (&temp_bitmap, &reg_obstack);
1232 calculate_local_reg_remat_bb_data ();
1233 calculate_livein_cands ();
1234 calculate_gen_cands ();
1235 bitmap_initialize (&all_blocks, &reg_obstack);
1236 FOR_ALL_BB_FN (bb, cfun)
1237 bitmap_set_bit (&all_blocks, bb->index);
1238 calculate_global_remat_bb_data ();
1239 dump_candidates_and_remat_bb_data ();
1240 result = do_remat ();
1241 all_cands.release ();
1242 bitmap_clear (&temp_bitmap);
1243 finish_remat_bb_data ();
1244 finish_cand_table ();
1245 bitmap_clear (&all_blocks);
1246 free (regno_cands);
1247 free (insn_to_cand);
1248 timevar_pop (TV_LRA_REMAT);
1249 return result;