2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lra-remat.c
blobb4f83d14f040f287777bc4854411d7f97877339d
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 "input.h"
69 #include "function.h"
70 #include "symtab.h"
71 #include "flags.h"
72 #include "alias.h"
73 #include "tree.h"
74 #include "expmed.h"
75 #include "dojump.h"
76 #include "explow.h"
77 #include "calls.h"
78 #include "emit-rtl.h"
79 #include "varasm.h"
80 #include "stmt.h"
81 #include "expr.h"
82 #include "predict.h"
83 #include "dominance.h"
84 #include "cfg.h"
85 #include "basic-block.h"
86 #include "except.h"
87 #include "df.h"
88 #include "ira.h"
89 #include "sparseset.h"
90 #include "params.h"
91 #include "lra-int.h"
93 /* Number of candidates for rematerialization. */
94 static unsigned int cands_num;
96 /* The following is used for representation of call_used_reg_set in
97 form array whose elements are hard register numbers with nonzero bit
98 in CALL_USED_REG_SET. */
99 static int call_used_regs_arr_len;
100 static int call_used_regs_arr[FIRST_PSEUDO_REGISTER];
102 /* Bitmap used for different calculations. */
103 static bitmap_head temp_bitmap;
105 typedef struct cand *cand_t;
106 typedef const struct cand *const_cand_t;
108 /* Insn candidates for rematerialization. The candidate insn should
109 have the following properies:
110 o no any memory (as access to memory is non-profitable)
111 o no INOUT regs (it means no non-paradoxical subreg of output reg)
112 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
113 o all other pseudos are with assigned hard regs. */
114 struct cand
116 /* Index of the candidates in all_cands. */
117 int index;
118 /* The candidate insn. */
119 rtx_insn *insn;
120 /* Insn pseudo regno for rematerialization. */
121 int regno;
122 /* Non-negative if a reload pseudo is in the insn instead of the
123 pseudo for rematerialization. */
124 int reload_regno;
125 /* Number of the operand containing the regno or its reload
126 regno. */
127 int nop;
128 /* Next candidate for the same regno. */
129 cand_t next_regno_cand;
132 /* Vector containing all candidates. */
133 static vec<cand_t> all_cands;
134 /* Map: insn -> candidate representing it. It is null if the insn can
135 not be used for rematerialization. */
136 static cand_t *insn_to_cand;
138 /* Map regno -> candidates can be used for the regno
139 rematerialization. */
140 static cand_t *regno_cands;
142 /* Data about basic blocks used for the rematerialization
143 sub-pass. */
144 struct remat_bb_data
146 /* Basic block about which the below data are. */
147 basic_block bb;
148 /* Registers changed in the basic block: */
149 bitmap_head changed_regs;
150 /* Registers becoming dead in the BB. */
151 bitmap_head dead_regs;
152 /* Cands present in the BB whose in/out regs are not changed after
153 the cands occurence and are not dead (except the reload
154 regno). */
155 bitmap_head gen_cands;
156 bitmap_head livein_cands; /* cands whose inputs live at the BB start. */
157 bitmap_head pavin_cands; /* cands partially available at BB entry. */
158 bitmap_head pavout_cands; /* cands partially available at BB exit. */
159 bitmap_head avin_cands; /* cands available at the entry of the BB. */
160 bitmap_head avout_cands; /* cands available at the exit of the BB. */
163 /* Array for all BB data. Indexed by the corresponding BB index. */
164 typedef struct remat_bb_data *remat_bb_data_t;
166 /* Basic blocks for data flow problems -- all bocks except the special
167 ones. */
168 static bitmap_head all_blocks;
170 /* All basic block data are referred through the following array. */
171 static remat_bb_data_t remat_bb_data;
173 /* Two small functions for access to the bb data. */
174 static inline remat_bb_data_t
175 get_remat_bb_data (basic_block bb)
177 return &remat_bb_data[(bb)->index];
180 static inline remat_bb_data_t
181 get_remat_bb_data_by_index (int index)
183 return &remat_bb_data[index];
188 /* Recursive hash function for RTL X. */
189 static hashval_t
190 rtx_hash (rtx x)
192 int i, j;
193 enum rtx_code code;
194 const char *fmt;
195 hashval_t val = 0;
197 if (x == 0)
198 return val;
200 code = GET_CODE (x);
201 val += (int) code + 4095;
203 /* Some RTL can be compared nonrecursively. */
204 switch (code)
206 case REG:
207 return val + REGNO (x);
209 case LABEL_REF:
210 return iterative_hash_object (XEXP (x, 0), val);
212 case SYMBOL_REF:
213 return iterative_hash_object (XSTR (x, 0), val);
215 case SCRATCH:
216 case CONST_DOUBLE:
217 case CONST_INT:
218 case CONST_VECTOR:
219 return val;
221 default:
222 break;
225 /* Hash the elements. */
226 fmt = GET_RTX_FORMAT (code);
227 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
229 switch (fmt[i])
231 case 'w':
232 val += XWINT (x, i);
233 break;
235 case 'n':
236 case 'i':
237 val += XINT (x, i);
238 break;
240 case 'V':
241 case 'E':
242 val += XVECLEN (x, i);
244 for (j = 0; j < XVECLEN (x, i); j++)
245 val += rtx_hash (XVECEXP (x, i, j));
246 break;
248 case 'e':
249 val += rtx_hash (XEXP (x, i));
250 break;
252 case 'S':
253 case 's':
254 val += htab_hash_string (XSTR (x, i));
255 break;
257 case 'u':
258 case '0':
259 case 't':
260 break;
262 /* It is believed that rtx's at this level will never
263 contain anything but integers and other rtx's, except for
264 within LABEL_REFs and SYMBOL_REFs. */
265 default:
266 abort ();
269 return val;
274 /* Hash table for the candidates. Different insns (e.g. structurally
275 the same insns or even insns with different unused output regs) can
276 be represented by the same candidate in the table. */
277 static htab_t cand_table;
279 /* Hash function for candidate CAND. */
280 static hashval_t
281 cand_hash (const void *cand)
283 const_cand_t c = (const_cand_t) cand;
284 lra_insn_recog_data_t id = lra_get_insn_recog_data (c->insn);
285 struct lra_static_insn_data *static_id = id->insn_static_data;
286 int nops = static_id->n_operands;
287 hashval_t hash = 0;
289 for (int i = 0; i < nops; i++)
290 if (i == c->nop)
291 hash = iterative_hash_object (c->regno, hash);
292 else if (static_id->operand[i].type == OP_IN)
293 hash = iterative_hash_object (*id->operand_loc[i], hash);
294 return hash;
297 /* Equal function for candidates CAND1 and CAND2. They are equal if
298 the corresponding candidate insns have the same code, the same
299 regno for rematerialization, the same input operands. */
300 static int
301 cand_eq_p (const void *cand1, const void *cand2)
303 const_cand_t c1 = (const_cand_t) cand1;
304 const_cand_t c2 = (const_cand_t) cand2;
305 lra_insn_recog_data_t id1 = lra_get_insn_recog_data (c1->insn);
306 lra_insn_recog_data_t id2 = lra_get_insn_recog_data (c2->insn);
307 struct lra_static_insn_data *static_id1 = id1->insn_static_data;
308 int nops = static_id1->n_operands;
310 if (c1->regno != c2->regno
311 || INSN_CODE (c1->insn) < 0
312 || INSN_CODE (c1->insn) != INSN_CODE (c2->insn))
313 return false;
314 gcc_assert (c1->nop == c2->nop);
315 for (int i = 0; i < nops; i++)
316 if (i != c1->nop && static_id1->operand[i].type == OP_IN
317 && *id1->operand_loc[i] != *id2->operand_loc[i])
318 return false;
319 return true;
322 /* Insert candidate CAND into the table if it is not there yet.
323 Return candidate which is in the table. */
324 static cand_t
325 insert_cand (cand_t cand)
327 void **entry_ptr;
329 entry_ptr = htab_find_slot (cand_table, cand, INSERT);
330 if (*entry_ptr == NULL)
331 *entry_ptr = (void *) cand;
332 return (cand_t) *entry_ptr;
335 /* Free candidate CAND memory. */
336 static void
337 free_cand (void *cand)
339 free (cand);
342 /* Initiate the candidate table. */
343 static void
344 initiate_cand_table (void)
346 cand_table = htab_create (8000, cand_hash, cand_eq_p,
347 (htab_del) free_cand);
350 /* Finish the candidate table. */
351 static void
352 finish_cand_table (void)
354 htab_delete (cand_table);
359 /* Return true if X contains memory or some UNSPEC. We can not just
360 check insn operands as memory or unspec might be not an operand
361 itself but contain an operand. Insn with memory access is not
362 profitable for rematerialization. Rematerialization of UNSPEC
363 might result in wrong code generation as the UNPEC effect is
364 unknown (e.g. generating a label). */
365 static bool
366 bad_for_rematerialization_p (rtx x)
368 int i, j;
369 const char *fmt;
370 enum rtx_code code;
372 if (MEM_P (x) || GET_CODE (x) == UNSPEC || GET_CODE (x) == UNSPEC_VOLATILE)
373 return true;
374 code = GET_CODE (x);
375 fmt = GET_RTX_FORMAT (code);
376 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
378 if (fmt[i] == 'e')
380 if (bad_for_rematerialization_p (XEXP (x, i)))
381 return true;
383 else if (fmt[i] == 'E')
385 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
386 if (bad_for_rematerialization_p (XVECEXP (x, i, j)))
387 return true;
390 return false;
393 /* If INSN can not be used for rematerialization, return negative
394 value. If INSN can be considered as a candidate for
395 rematerialization, return value which is the operand number of the
396 pseudo for which the insn can be used for rematerialization. Here
397 we consider the insns without any memory, spilled pseudo (except
398 for the rematerialization pseudo), or dying or unused regs. */
399 static int
400 operand_to_remat (rtx_insn *insn)
402 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
403 struct lra_static_insn_data *static_id = id->insn_static_data;
404 struct lra_insn_reg *reg, *found_reg = NULL;
406 /* Don't rematerialize insns which can change PC. */
407 if (JUMP_P (insn) || CALL_P (insn))
408 return -1;
409 /* First find a pseudo which can be rematerialized. */
410 for (reg = id->regs; reg != NULL; reg = reg->next)
411 /* True FRAME_POINTER_NEEDED might be because we can not follow
412 changing sp offsets, e.g. alloca is used. If the insn contains
413 stack pointer in such case, we can not rematerialize it as we
414 can not know sp offset at a rematerialization place. */
415 if (reg->regno == STACK_POINTER_REGNUM && frame_pointer_needed)
416 return -1;
417 else if (reg->type == OP_OUT && ! reg->subreg_p
418 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
420 /* We permits only one spilled reg. */
421 if (found_reg != NULL)
422 return -1;
423 found_reg = reg;
425 if (found_reg == NULL)
426 return -1;
427 if (found_reg->regno < FIRST_PSEUDO_REGISTER)
428 return -1;
429 if (bad_for_rematerialization_p (PATTERN (insn)))
430 return -1;
431 /* Check the other regs are not spilled. */
432 for (reg = id->regs; reg != NULL; reg = reg->next)
433 if (found_reg == reg)
434 continue;
435 else if (reg->type == OP_INOUT)
436 return -1;
437 else if (reg->regno >= FIRST_PSEUDO_REGISTER
438 && reg_renumber[reg->regno] < 0)
439 /* Another spilled reg. */
440 return -1;
441 else if (reg->type == OP_IN)
443 if (find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
444 /* We don't want to make live ranges longer. */
445 return -1;
446 /* Check that there is no output reg as the input one. */
447 for (struct lra_insn_reg *reg2 = id->regs;
448 reg2 != NULL;
449 reg2 = reg2->next)
450 if (reg2->type == OP_OUT && reg->regno == reg2->regno)
451 return -1;
452 if (reg->regno < FIRST_PSEUDO_REGISTER)
453 for (struct lra_insn_reg *reg2 = static_id->hard_regs;
454 reg2 != NULL;
455 reg2 = reg2->next)
456 if (reg2->type == OP_OUT
457 && reg->regno <= reg2->regno
458 && (reg2->regno
459 < (reg->regno
460 + hard_regno_nregs[reg->regno][reg->biggest_mode])))
461 return -1;
463 /* Find the rematerialization operand. */
464 int nop = static_id->n_operands;
465 for (int i = 0; i < nop; i++)
466 if (REG_P (*id->operand_loc[i])
467 && (int) REGNO (*id->operand_loc[i]) == found_reg->regno)
468 return i;
469 return -1;
472 /* Create candidate for INSN with rematerialization operand NOP and
473 REGNO. Insert the candidate into the table and set up the
474 corresponding INSN_TO_CAND element. */
475 static void
476 create_cand (rtx_insn *insn, int nop, int regno)
478 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
479 rtx reg = *id->operand_loc[nop];
480 gcc_assert (REG_P (reg));
481 int op_regno = REGNO (reg);
482 gcc_assert (op_regno >= FIRST_PSEUDO_REGISTER);
483 cand_t cand = XNEW (struct cand);
484 cand->insn = insn;
485 cand->nop = nop;
486 cand->regno = regno;
487 cand->reload_regno = op_regno == regno ? -1 : op_regno;
488 gcc_assert (cand->regno >= 0);
489 cand_t cand_in_table = insert_cand (cand);
490 insn_to_cand[INSN_UID (insn)] = cand_in_table;
491 if (cand != cand_in_table)
492 free (cand);
493 else
495 /* A new cand. */
496 cand->index = all_cands.length ();
497 all_cands.safe_push (cand);
498 cand->next_regno_cand = regno_cands[cand->regno];
499 regno_cands[cand->regno] = cand;
503 /* Create rematerialization candidates (inserting them into the
504 table). */
505 static void
506 create_cands (void)
508 rtx_insn *insn;
509 struct potential_cand
511 rtx_insn *insn;
512 int nop;
514 struct potential_cand *regno_potential_cand;
516 /* Create candidates. */
517 regno_potential_cand = XCNEWVEC (struct potential_cand, max_reg_num ());
518 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
519 if (INSN_P (insn))
521 rtx set;
522 int src_regno, dst_regno;
523 rtx_insn *insn2;
524 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
525 int nop = operand_to_remat (insn);
526 int regno = -1;
528 if ((set = single_set (insn)) != NULL
529 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
530 && ((src_regno = REGNO (SET_SRC (set)))
531 >= lra_constraint_new_regno_start)
532 && (dst_regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
533 && reg_renumber[dst_regno] < 0
534 && (insn2 = regno_potential_cand[src_regno].insn) != NULL
535 && BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn))
536 /* It is an output reload insn after insn can be
537 rematerialized (potential candidate). */
538 create_cand (insn2, regno_potential_cand[src_regno].nop, dst_regno);
539 if (nop < 0)
540 goto fail;
541 gcc_assert (REG_P (*id->operand_loc[nop]));
542 regno = REGNO (*id->operand_loc[nop]);
543 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
544 if (reg_renumber[regno] < 0)
545 create_cand (insn, nop, regno);
546 else
548 regno_potential_cand[regno].insn = insn;
549 regno_potential_cand[regno].nop = nop;
550 goto fail;
552 regno = -1;
553 fail:
554 for (struct lra_insn_reg *reg = id->regs; reg != NULL; reg = reg->next)
555 if (reg->type != OP_IN && reg->regno != regno
556 && reg->regno >= FIRST_PSEUDO_REGISTER)
557 regno_potential_cand[reg->regno].insn = NULL;
559 cands_num = all_cands.length ();
560 free (regno_potential_cand);
565 /* Create and initialize BB data. */
566 static void
567 create_remat_bb_data (void)
569 basic_block bb;
570 remat_bb_data_t bb_info;
572 remat_bb_data = XNEWVEC (struct remat_bb_data,
573 last_basic_block_for_fn (cfun));
574 FOR_ALL_BB_FN (bb, cfun)
576 #ifdef ENABLE_CHECKING
577 if (bb->index < 0 || bb->index >= last_basic_block_for_fn (cfun))
578 abort ();
579 #endif
580 bb_info = get_remat_bb_data (bb);
581 bb_info->bb = bb;
582 bitmap_initialize (&bb_info->changed_regs, &reg_obstack);
583 bitmap_initialize (&bb_info->dead_regs, &reg_obstack);
584 bitmap_initialize (&bb_info->gen_cands, &reg_obstack);
585 bitmap_initialize (&bb_info->livein_cands, &reg_obstack);
586 bitmap_initialize (&bb_info->pavin_cands, &reg_obstack);
587 bitmap_initialize (&bb_info->pavout_cands, &reg_obstack);
588 bitmap_initialize (&bb_info->avin_cands, &reg_obstack);
589 bitmap_initialize (&bb_info->avout_cands, &reg_obstack);
593 /* Dump all candidates to DUMP_FILE. */
594 static void
595 dump_cands (FILE *dump_file)
597 int i;
598 cand_t cand;
600 fprintf (dump_file, "\nCands:\n");
601 for (i = 0; i < (int) cands_num; i++)
603 cand = all_cands[i];
604 fprintf (dump_file, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
605 i, cand->nop, cand->regno, cand->reload_regno);
606 print_inline_rtx (dump_file, cand->insn, 6);
607 fprintf (dump_file, "\n");
611 /* Dump all candidates and BB data. */
612 static void
613 dump_candidates_and_remat_bb_data (void)
615 basic_block bb;
617 if (lra_dump_file == NULL)
618 return;
619 dump_cands (lra_dump_file);
620 FOR_EACH_BB_FN (bb, cfun)
622 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
623 /* Livein */
624 fprintf (lra_dump_file, " register live in:");
625 dump_regset (df_get_live_in (bb), lra_dump_file);
626 putc ('\n', lra_dump_file);
627 /* Liveout */
628 fprintf (lra_dump_file, " register live out:");
629 dump_regset (df_get_live_out (bb), lra_dump_file);
630 putc ('\n', lra_dump_file);
631 /* Changed/dead regs: */
632 fprintf (lra_dump_file, " changed regs:");
633 dump_regset (&get_remat_bb_data (bb)->changed_regs, lra_dump_file);
634 putc ('\n', lra_dump_file);
635 fprintf (lra_dump_file, " dead regs:");
636 dump_regset (&get_remat_bb_data (bb)->dead_regs, lra_dump_file);
637 putc ('\n', lra_dump_file);
638 lra_dump_bitmap_with_title ("cands generated in BB",
639 &get_remat_bb_data (bb)->gen_cands, bb->index);
640 lra_dump_bitmap_with_title ("livein cands in BB",
641 &get_remat_bb_data (bb)->livein_cands, bb->index);
642 lra_dump_bitmap_with_title ("pavin cands in BB",
643 &get_remat_bb_data (bb)->pavin_cands, bb->index);
644 lra_dump_bitmap_with_title ("pavout cands in BB",
645 &get_remat_bb_data (bb)->pavout_cands, bb->index);
646 lra_dump_bitmap_with_title ("avin cands in BB",
647 &get_remat_bb_data (bb)->avin_cands, bb->index);
648 lra_dump_bitmap_with_title ("avout cands in BB",
649 &get_remat_bb_data (bb)->avout_cands, bb->index);
653 /* Free all BB data. */
654 static void
655 finish_remat_bb_data (void)
657 basic_block bb;
659 FOR_EACH_BB_FN (bb, cfun)
661 bitmap_clear (&get_remat_bb_data (bb)->avout_cands);
662 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
663 bitmap_clear (&get_remat_bb_data (bb)->pavout_cands);
664 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
665 bitmap_clear (&get_remat_bb_data (bb)->livein_cands);
666 bitmap_clear (&get_remat_bb_data (bb)->gen_cands);
667 bitmap_clear (&get_remat_bb_data (bb)->dead_regs);
668 bitmap_clear (&get_remat_bb_data (bb)->changed_regs);
670 free (remat_bb_data);
675 /* Update changed_regs and dead_regs of BB from INSN. */
676 static void
677 set_bb_regs (basic_block bb, rtx_insn *insn)
679 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
680 struct lra_insn_reg *reg;
682 for (reg = id->regs; reg != NULL; reg = reg->next)
683 if (reg->type != OP_IN)
684 bitmap_set_bit (&get_remat_bb_data (bb)->changed_regs, reg->regno);
685 else
687 if (find_regno_note (insn, REG_DEAD, (unsigned) reg->regno) != NULL)
688 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs, reg->regno);
690 if (CALL_P (insn))
691 for (int i = 0; i < call_used_regs_arr_len; i++)
692 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs,
693 call_used_regs_arr[i]);
696 /* Calculate changed_regs and dead_regs for each BB. */
697 static void
698 calculate_local_reg_remat_bb_data (void)
700 basic_block bb;
701 rtx_insn *insn;
703 FOR_EACH_BB_FN (bb, cfun)
704 FOR_BB_INSNS (bb, insn)
705 if (INSN_P (insn))
706 set_bb_regs (bb, insn);
711 /* Return true if REGNO is an input operand of INSN. */
712 static bool
713 input_regno_present_p (rtx_insn *insn, int regno)
715 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
716 struct lra_insn_reg *reg;
718 for (reg = id->regs; reg != NULL; reg = reg->next)
719 if (reg->type == OP_IN && reg->regno == regno)
720 return true;
721 return false;
724 /* Return true if a call used register is an input operand of INSN. */
725 static bool
726 call_used_input_regno_present_p (rtx_insn *insn)
728 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
729 struct lra_insn_reg *reg;
731 for (reg = id->regs; reg != NULL; reg = reg->next)
732 if (reg->type == OP_IN && reg->regno <= FIRST_PSEUDO_REGISTER
733 && TEST_HARD_REG_BIT (call_used_reg_set, reg->regno))
734 return true;
735 return false;
738 /* Calculate livein_cands for each BB. */
739 static void
740 calculate_livein_cands (void)
742 basic_block bb;
744 FOR_EACH_BB_FN (bb, cfun)
746 bitmap livein_regs = df_get_live_in (bb);
747 bitmap livein_cands = &get_remat_bb_data (bb)->livein_cands;
748 for (unsigned int i = 0; i < cands_num; i++)
750 cand_t cand = all_cands[i];
751 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
752 struct lra_insn_reg *reg;
754 for (reg = id->regs; reg != NULL; reg = reg->next)
755 if (reg->type == OP_IN && ! bitmap_bit_p (livein_regs, reg->regno))
756 break;
757 if (reg == NULL)
758 bitmap_set_bit (livein_cands, i);
763 /* Calculate gen_cands for each BB. */
764 static void
765 calculate_gen_cands (void)
767 basic_block bb;
768 bitmap gen_cands;
769 bitmap_head gen_insns;
770 rtx_insn *insn;
772 bitmap_initialize (&gen_insns, &reg_obstack);
773 FOR_EACH_BB_FN (bb, cfun)
775 gen_cands = &get_remat_bb_data (bb)->gen_cands;
776 bitmap_clear (&gen_insns);
777 FOR_BB_INSNS (bb, insn)
778 if (INSN_P (insn))
780 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
781 struct lra_insn_reg *reg;
782 unsigned int uid;
783 bitmap_iterator bi;
784 cand_t cand;
785 rtx set;
786 int src_regno = -1, dst_regno = -1;
788 if ((set = single_set (insn)) != NULL
789 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
791 src_regno = REGNO (SET_SRC (set));
792 dst_regno = REGNO (SET_DEST (set));
795 /* Update gen_cands: */
796 bitmap_clear (&temp_bitmap);
797 for (reg = id->regs; reg != NULL; reg = reg->next)
798 if (reg->type != OP_IN
799 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
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 /* Ignore the reload insn. */
807 if (src_regno == cand->reload_regno
808 && dst_regno == cand->regno)
809 continue;
810 if (cand->regno == reg->regno
811 || input_regno_present_p (insn2, reg->regno))
813 bitmap_clear_bit (gen_cands, cand->index);
814 bitmap_set_bit (&temp_bitmap, uid);
818 if (CALL_P (insn))
819 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
821 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
823 cand = insn_to_cand[INSN_UID (insn2)];
824 gcc_assert (cand != NULL);
825 if (call_used_input_regno_present_p (insn2))
827 bitmap_clear_bit (gen_cands, cand->index);
828 bitmap_set_bit (&temp_bitmap, uid);
831 bitmap_and_compl_into (&gen_insns, &temp_bitmap);
833 cand = insn_to_cand[INSN_UID (insn)];
834 if (cand != NULL)
836 bitmap_set_bit (gen_cands, cand->index);
837 bitmap_set_bit (&gen_insns, INSN_UID (insn));
841 bitmap_clear (&gen_insns);
846 /* The common transfer function used by the DF equation solver to
847 propagate (partial) availability info BB_IN to BB_OUT through block
848 with BB_INDEX according to the following equation:
850 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
852 static bool
853 cand_trans_fun (int bb_index, bitmap bb_in, bitmap bb_out)
855 remat_bb_data_t bb_info;
856 bitmap bb_livein, bb_changed_regs, bb_dead_regs;
857 unsigned int cid;
858 bitmap_iterator bi;
860 bb_info = get_remat_bb_data_by_index (bb_index);
861 bb_livein = &bb_info->livein_cands;
862 bb_changed_regs = &bb_info->changed_regs;
863 bb_dead_regs = &bb_info->dead_regs;
864 /* Calculate killed avin cands -- cands whose regs are changed or
865 becoming dead in the BB. We calculate it here as we hope that
866 repeated calculations are compensated by smaller size of BB_IN in
867 comparison with all candidates number. */
868 bitmap_clear (&temp_bitmap);
869 EXECUTE_IF_SET_IN_BITMAP (bb_in, 0, cid, bi)
871 cand_t cand = all_cands[cid];
872 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
873 struct lra_insn_reg *reg;
875 if (! bitmap_bit_p (bb_livein, cid))
877 bitmap_set_bit (&temp_bitmap, cid);
878 continue;
880 for (reg = id->regs; reg != NULL; reg = reg->next)
881 /* Ignore all outputs which are not the regno for
882 rematerialization. */
883 if (reg->type == OP_OUT && reg->regno != cand->regno)
884 continue;
885 else if (bitmap_bit_p (bb_changed_regs, reg->regno)
886 || bitmap_bit_p (bb_dead_regs, reg->regno))
888 bitmap_set_bit (&temp_bitmap, cid);
889 break;
891 /* Check regno for rematerialization. */
892 if (bitmap_bit_p (bb_changed_regs, cand->regno)
893 || bitmap_bit_p (bb_dead_regs, cand->regno))
894 bitmap_set_bit (&temp_bitmap, cid);
896 return bitmap_ior_and_compl (bb_out,
897 &bb_info->gen_cands, bb_in, &temp_bitmap);
902 /* The transfer function used by the DF equation solver to propagate
903 partial candidate availability info through block with BB_INDEX
904 according to the following equation:
906 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
908 static bool
909 cand_pav_trans_fun (int bb_index)
911 remat_bb_data_t bb_info;
913 bb_info = get_remat_bb_data_by_index (bb_index);
914 return cand_trans_fun (bb_index, &bb_info->pavin_cands,
915 &bb_info->pavout_cands);
918 /* The confluence function used by the DF equation solver to set up
919 cand_pav info for a block BB without predecessor. */
920 static void
921 cand_pav_con_fun_0 (basic_block bb)
923 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
926 /* The confluence function used by the DF equation solver to propagate
927 partial candidate availability info from predecessor to successor
928 on edge E (pred->bb) according to the following equation:
930 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
932 static bool
933 cand_pav_con_fun_n (edge e)
935 basic_block pred = e->src;
936 basic_block bb = e->dest;
937 remat_bb_data_t bb_info;
938 bitmap bb_pavin, pred_pavout;
940 bb_info = get_remat_bb_data (bb);
941 bb_pavin = &bb_info->pavin_cands;
942 pred_pavout = &get_remat_bb_data (pred)->pavout_cands;
943 return bitmap_ior_into (bb_pavin, pred_pavout);
948 /* The transfer function used by the DF equation solver to propagate
949 candidate availability info through block with BB_INDEX according
950 to the following equation:
952 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
954 static bool
955 cand_av_trans_fun (int bb_index)
957 remat_bb_data_t bb_info;
959 bb_info = get_remat_bb_data_by_index (bb_index);
960 return cand_trans_fun (bb_index, &bb_info->avin_cands,
961 &bb_info->avout_cands);
964 /* The confluence function used by the DF equation solver to set up
965 cand_av info for a block BB without predecessor. */
966 static void
967 cand_av_con_fun_0 (basic_block bb)
969 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
972 /* The confluence function used by the DF equation solver to propagate
973 cand_av info from predecessor to successor on edge E (pred->bb)
974 according to the following equation:
976 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
978 static bool
979 cand_av_con_fun_n (edge e)
981 basic_block pred = e->src;
982 basic_block bb = e->dest;
983 remat_bb_data_t bb_info;
984 bitmap bb_avin, pred_avout;
986 bb_info = get_remat_bb_data (bb);
987 bb_avin = &bb_info->avin_cands;
988 pred_avout = &get_remat_bb_data (pred)->avout_cands;
989 return bitmap_and_into (bb_avin, pred_avout);
992 /* Calculate available candidates for each BB. */
993 static void
994 calculate_global_remat_bb_data (void)
996 basic_block bb;
998 df_simple_dataflow
999 (DF_FORWARD, NULL, cand_pav_con_fun_0, cand_pav_con_fun_n,
1000 cand_pav_trans_fun, &all_blocks,
1001 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
1002 /* Initialize avin by pavin. */
1003 FOR_EACH_BB_FN (bb, cfun)
1004 bitmap_copy (&get_remat_bb_data (bb)->avin_cands,
1005 &get_remat_bb_data (bb)->pavin_cands);
1006 df_simple_dataflow
1007 (DF_FORWARD, NULL, cand_av_con_fun_0, cand_av_con_fun_n,
1008 cand_av_trans_fun, &all_blocks,
1009 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
1014 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
1015 static void
1016 change_sp_offset (rtx_insn *insns, HOST_WIDE_INT sp_offset)
1018 for (rtx_insn *insn = insns; insn != NULL; insn = NEXT_INSN (insn))
1019 eliminate_regs_in_insn (insn, false, false, sp_offset);
1022 /* Return start hard register of REG (can be a hard or a pseudo reg)
1023 or -1 (if it is a spilled pseudo). Return number of hard registers
1024 occupied by REG through parameter NREGS if the start hard reg is
1025 not negative. */
1026 static int
1027 get_hard_regs (struct lra_insn_reg *reg, int &nregs)
1029 int regno = reg->regno;
1030 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
1032 if (hard_regno >= 0)
1033 nregs = hard_regno_nregs[hard_regno][reg->biggest_mode];
1034 return hard_regno;
1037 /* Make copy of and register scratch pseudos in rematerialized insn
1038 REMAT_INSN. */
1039 static void
1040 update_scratch_ops (rtx_insn *remat_insn)
1042 lra_insn_recog_data_t id = lra_get_insn_recog_data (remat_insn);
1043 struct lra_static_insn_data *static_id = id->insn_static_data;
1044 for (int i = 0; i < static_id->n_operands; i++)
1046 rtx *loc = id->operand_loc[i];
1047 if (! REG_P (*loc))
1048 continue;
1049 int regno = REGNO (*loc);
1050 if (! lra_former_scratch_p (regno))
1051 continue;
1052 *loc = lra_create_new_reg (GET_MODE (*loc), *loc,
1053 lra_get_allocno_class (regno),
1054 "scratch pseudo copy");
1055 lra_register_new_scratch_op (remat_insn, i);
1060 /* Insert rematerialization insns using the data-flow data calculated
1061 earlier. */
1062 static bool
1063 do_remat (void)
1065 rtx_insn *insn;
1066 basic_block bb;
1067 bitmap_head avail_cands;
1068 bool changed_p = false;
1069 /* Living hard regs and hard registers of living pseudos. */
1070 HARD_REG_SET live_hard_regs;
1072 bitmap_initialize (&avail_cands, &reg_obstack);
1073 FOR_EACH_BB_FN (bb, cfun)
1075 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (bb));
1076 bitmap_and (&avail_cands, &get_remat_bb_data (bb)->avin_cands,
1077 &get_remat_bb_data (bb)->livein_cands);
1078 FOR_BB_INSNS (bb, insn)
1080 if (!NONDEBUG_INSN_P (insn))
1081 continue;
1083 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1084 struct lra_static_insn_data *static_id = id->insn_static_data;
1085 struct lra_insn_reg *reg;
1086 cand_t cand;
1087 unsigned int cid;
1088 bitmap_iterator bi;
1089 rtx set;
1090 int src_regno = -1, dst_regno = -1;
1092 if ((set = single_set (insn)) != NULL
1093 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
1095 src_regno = REGNO (SET_SRC (set));
1096 dst_regno = REGNO (SET_DEST (set));
1099 cand = NULL;
1100 /* Check possibility of rematerialization (hard reg or
1101 unpsilled pseudo <- spilled pseudo): */
1102 if (dst_regno >= 0 && src_regno >= FIRST_PSEUDO_REGISTER
1103 && reg_renumber[src_regno] < 0
1104 && (dst_regno < FIRST_PSEUDO_REGISTER
1105 || reg_renumber[dst_regno] >= 0))
1107 for (cand = regno_cands[src_regno];
1108 cand != NULL;
1109 cand = cand->next_regno_cand)
1110 if (bitmap_bit_p (&avail_cands, cand->index))
1111 break;
1113 int i, hard_regno, nregs;
1114 rtx_insn *remat_insn = NULL;
1115 HOST_WIDE_INT cand_sp_offset = 0;
1116 if (cand != NULL)
1118 lra_insn_recog_data_t cand_id
1119 = lra_get_insn_recog_data (cand->insn);
1120 struct lra_static_insn_data *static_cand_id
1121 = cand_id->insn_static_data;
1122 rtx saved_op = *cand_id->operand_loc[cand->nop];
1124 /* Check clobbers do not kill something living. */
1125 gcc_assert (REG_P (saved_op));
1126 int ignore_regno = REGNO (saved_op);
1128 for (reg = cand_id->regs; reg != NULL; reg = reg->next)
1129 if (reg->type != OP_IN && reg->regno != ignore_regno)
1131 hard_regno = get_hard_regs (reg, nregs);
1132 gcc_assert (hard_regno >= 0);
1133 for (i = 0; i < nregs; i++)
1134 if (TEST_HARD_REG_BIT (live_hard_regs, hard_regno + i))
1135 break;
1136 if (i < nregs)
1137 break;
1140 if (reg == NULL)
1142 for (reg = static_cand_id->hard_regs;
1143 reg != NULL;
1144 reg = reg->next)
1145 if (reg->type != OP_IN
1146 && TEST_HARD_REG_BIT (live_hard_regs, reg->regno))
1147 break;
1150 if (reg == NULL)
1152 *cand_id->operand_loc[cand->nop] = SET_DEST (set);
1153 lra_update_insn_regno_info (cand->insn);
1154 bool ok_p = lra_constrain_insn (cand->insn);
1155 if (ok_p)
1157 rtx remat_pat = copy_insn (PATTERN (cand->insn));
1159 start_sequence ();
1160 emit_insn (remat_pat);
1161 remat_insn = get_insns ();
1162 end_sequence ();
1163 if (recog_memoized (remat_insn) < 0)
1164 remat_insn = NULL;
1165 cand_sp_offset = cand_id->sp_offset;
1167 *cand_id->operand_loc[cand->nop] = saved_op;
1168 lra_update_insn_regno_info (cand->insn);
1172 bitmap_clear (&temp_bitmap);
1173 /* Update avail_cands (see analogous code for
1174 calculate_gen_cands). */
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)
1178 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1180 cand = all_cands[cid];
1182 /* Ignore the reload insn. */
1183 if (src_regno == cand->reload_regno
1184 && dst_regno == cand->regno)
1185 continue;
1186 if (cand->regno == reg->regno
1187 || input_regno_present_p (cand->insn, reg->regno))
1188 bitmap_set_bit (&temp_bitmap, cand->index);
1191 if (CALL_P (insn))
1192 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1194 cand = all_cands[cid];
1196 if (call_used_input_regno_present_p (cand->insn))
1197 bitmap_set_bit (&temp_bitmap, cand->index);
1200 bitmap_and_compl_into (&avail_cands, &temp_bitmap);
1201 if ((cand = insn_to_cand[INSN_UID (insn)]) != NULL)
1202 bitmap_set_bit (&avail_cands, cand->index);
1204 if (remat_insn != NULL)
1206 HOST_WIDE_INT sp_offset_change = cand_sp_offset - id->sp_offset;
1207 if (sp_offset_change != 0)
1208 change_sp_offset (remat_insn, sp_offset_change);
1209 update_scratch_ops (remat_insn);
1210 lra_process_new_insns (insn, remat_insn, NULL,
1211 "Inserting rematerialization insn");
1212 lra_set_insn_deleted (insn);
1213 changed_p = true;
1214 continue;
1217 /* Update live hard regs: */
1218 for (reg = id->regs; reg != NULL; reg = reg->next)
1219 if (reg->type == OP_IN
1220 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1222 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1223 continue;
1224 for (i = 0; i < nregs; i++)
1225 CLEAR_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1227 /* Process also hard regs (e.g. CC register) which are part
1228 of insn definition. */
1229 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1230 if (reg->type == OP_IN
1231 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1232 CLEAR_HARD_REG_BIT (live_hard_regs, reg->regno);
1233 /* Inputs have been processed, now process outputs. */
1234 for (reg = id->regs; reg != NULL; reg = reg->next)
1235 if (reg->type != OP_IN
1236 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1238 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1239 continue;
1240 for (i = 0; i < nregs; i++)
1241 SET_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1243 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1244 if (reg->type != OP_IN
1245 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1246 SET_HARD_REG_BIT (live_hard_regs, reg->regno);
1249 bitmap_clear (&avail_cands);
1250 return changed_p;
1255 /* Current number of rematerialization iteration. */
1256 int lra_rematerialization_iter;
1258 /* Entry point of the rematerialization sub-pass. Return true if we
1259 did any rematerialization. */
1260 bool
1261 lra_remat (void)
1263 basic_block bb;
1264 bool result;
1265 int max_regno = max_reg_num ();
1267 if (! flag_lra_remat)
1268 return false;
1269 lra_rematerialization_iter++;
1270 if (lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
1271 return false;
1272 if (lra_dump_file != NULL)
1273 fprintf (lra_dump_file,
1274 "\n******** Rematerialization #%d: ********\n\n",
1275 lra_rematerialization_iter);
1276 timevar_push (TV_LRA_REMAT);
1277 insn_to_cand = XCNEWVEC (cand_t, get_max_uid ());
1278 regno_cands = XCNEWVEC (cand_t, max_regno);
1279 all_cands.create (8000);
1280 call_used_regs_arr_len = 0;
1281 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1282 if (call_used_regs[i])
1283 call_used_regs_arr[call_used_regs_arr_len++] = i;
1284 initiate_cand_table ();
1285 create_cands ();
1286 create_remat_bb_data ();
1287 bitmap_initialize (&temp_bitmap, &reg_obstack);
1288 calculate_local_reg_remat_bb_data ();
1289 calculate_livein_cands ();
1290 calculate_gen_cands ();
1291 bitmap_initialize (&all_blocks, &reg_obstack);
1292 FOR_ALL_BB_FN (bb, cfun)
1293 bitmap_set_bit (&all_blocks, bb->index);
1294 calculate_global_remat_bb_data ();
1295 dump_candidates_and_remat_bb_data ();
1296 result = do_remat ();
1297 all_cands.release ();
1298 bitmap_clear (&temp_bitmap);
1299 finish_remat_bb_data ();
1300 finish_cand_table ();
1301 bitmap_clear (&all_blocks);
1302 free (regno_cands);
1303 free (insn_to_cand);
1304 timevar_pop (TV_LRA_REMAT);
1305 return result;