jit: API change to gcc_jit_context_new_global
[official-gcc.git] / gcc / lra-remat.c
bloba32ecca8086e39fe90d291dd16d597242f07f342
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 "symtab.h"
75 #include "expr.h"
76 #include "predict.h"
77 #include "dominance.h"
78 #include "cfg.h"
79 #include "basic-block.h"
80 #include "except.h"
81 #include "df.h"
82 #include "ira.h"
83 #include "sparseset.h"
84 #include "params.h"
85 #include "df.h"
86 #include "lra-int.h"
88 /* Number of candidates for rematerialization. */
89 static unsigned int cands_num;
91 /* The following is used for representation of call_used_reg_set in
92 form array whose elements are hard register numbers with nonzero bit
93 in CALL_USED_REG_SET. */
94 static int call_used_regs_arr_len;
95 static int call_used_regs_arr[FIRST_PSEUDO_REGISTER];
97 /* Bitmap used for different calculations. */
98 static bitmap_head temp_bitmap;
100 typedef struct cand *cand_t;
101 typedef const struct cand *const_cand_t;
103 /* Insn candidates for rematerialization. The candidate insn should
104 have the following properies:
105 o no any memory (as access to memory is non-profitable)
106 o no INOUT regs (it means no non-paradoxical subreg of output reg)
107 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
108 o all other pseudos are with assigned hard regs. */
109 struct cand
111 /* Index of the candidates in all_cands. */
112 int index;
113 /* The candidate insn. */
114 rtx_insn *insn;
115 /* Insn pseudo regno for rematerialization. */
116 int regno;
117 /* Non-negative if a reload pseudo is in the insn instead of the
118 pseudo for rematerialization. */
119 int reload_regno;
120 /* Number of the operand containing the regno or its reload
121 regno. */
122 int nop;
123 /* Next candidate for the same regno. */
124 cand_t next_regno_cand;
127 /* Vector containing all candidates. */
128 static vec<cand_t> all_cands;
129 /* Map: insn -> candidate representing it. It is null if the insn can
130 not be used for rematerialization. */
131 static cand_t *insn_to_cand;
133 /* Map regno -> candidates can be used for the regno
134 rematerialization. */
135 static cand_t *regno_cands;
137 /* Data about basic blocks used for the rematerialization
138 sub-pass. */
139 struct remat_bb_data
141 /* Basic block about which the below data are. */
142 basic_block bb;
143 /* Registers changed in the basic block: */
144 bitmap_head changed_regs;
145 /* Registers becoming dead in the BB. */
146 bitmap_head dead_regs;
147 /* Cands present in the BB whose in/out regs are not changed after
148 the cands occurence and are not dead (except the reload
149 regno). */
150 bitmap_head gen_cands;
151 bitmap_head livein_cands; /* cands whose inputs live at the BB start. */
152 bitmap_head pavin_cands; /* cands partially available at BB entry. */
153 bitmap_head pavout_cands; /* cands partially available at BB exit. */
154 bitmap_head avin_cands; /* cands available at the entry of the BB. */
155 bitmap_head avout_cands; /* cands available at the exit of the BB. */
158 /* Array for all BB data. Indexed by the corresponding BB index. */
159 typedef struct remat_bb_data *remat_bb_data_t;
161 /* Basic blocks for data flow problems -- all bocks except the special
162 ones. */
163 static bitmap_head all_blocks;
165 /* All basic block data are referred through the following array. */
166 static remat_bb_data_t remat_bb_data;
168 /* Two small functions for access to the bb data. */
169 static inline remat_bb_data_t
170 get_remat_bb_data (basic_block bb)
172 return &remat_bb_data[(bb)->index];
175 static inline remat_bb_data_t
176 get_remat_bb_data_by_index (int index)
178 return &remat_bb_data[index];
183 /* Recursive hash function for RTL X. */
184 static hashval_t
185 rtx_hash (rtx x)
187 int i, j;
188 enum rtx_code code;
189 const char *fmt;
190 hashval_t val = 0;
192 if (x == 0)
193 return val;
195 code = GET_CODE (x);
196 val += (int) code + 4095;
198 /* Some RTL can be compared nonrecursively. */
199 switch (code)
201 case REG:
202 return val + REGNO (x);
204 case LABEL_REF:
205 return iterative_hash_object (XEXP (x, 0), val);
207 case SYMBOL_REF:
208 return iterative_hash_object (XSTR (x, 0), val);
210 case SCRATCH:
211 case CONST_DOUBLE:
212 case CONST_INT:
213 case CONST_VECTOR:
214 return val;
216 default:
217 break;
220 /* Hash the elements. */
221 fmt = GET_RTX_FORMAT (code);
222 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
224 switch (fmt[i])
226 case 'w':
227 val += XWINT (x, i);
228 break;
230 case 'n':
231 case 'i':
232 val += XINT (x, i);
233 break;
235 case 'V':
236 case 'E':
237 val += XVECLEN (x, i);
239 for (j = 0; j < XVECLEN (x, i); j++)
240 val += rtx_hash (XVECEXP (x, i, j));
241 break;
243 case 'e':
244 val += rtx_hash (XEXP (x, i));
245 break;
247 case 'S':
248 case 's':
249 val += htab_hash_string (XSTR (x, i));
250 break;
252 case 'u':
253 case '0':
254 case 't':
255 break;
257 /* It is believed that rtx's at this level will never
258 contain anything but integers and other rtx's, except for
259 within LABEL_REFs and SYMBOL_REFs. */
260 default:
261 abort ();
264 return val;
269 /* Hash table for the candidates. Different insns (e.g. structurally
270 the same insns or even insns with different unused output regs) can
271 be represented by the same candidate in the table. */
272 static htab_t cand_table;
274 /* Hash function for candidate CAND. */
275 static hashval_t
276 cand_hash (const void *cand)
278 const_cand_t c = (const_cand_t) cand;
279 lra_insn_recog_data_t id = lra_get_insn_recog_data (c->insn);
280 struct lra_static_insn_data *static_id = id->insn_static_data;
281 int nops = static_id->n_operands;
282 hashval_t hash = 0;
284 for (int i = 0; i < nops; i++)
285 if (i == c->nop)
286 hash = iterative_hash_object (c->regno, hash);
287 else if (static_id->operand[i].type == OP_IN)
288 hash = iterative_hash_object (*id->operand_loc[i], hash);
289 return hash;
292 /* Equal function for candidates CAND1 and CAND2. They are equal if
293 the corresponding candidate insns have the same code, the same
294 regno for rematerialization, the same input operands. */
295 static int
296 cand_eq_p (const void *cand1, const void *cand2)
298 const_cand_t c1 = (const_cand_t) cand1;
299 const_cand_t c2 = (const_cand_t) cand2;
300 lra_insn_recog_data_t id1 = lra_get_insn_recog_data (c1->insn);
301 lra_insn_recog_data_t id2 = lra_get_insn_recog_data (c2->insn);
302 struct lra_static_insn_data *static_id1 = id1->insn_static_data;
303 int nops = static_id1->n_operands;
305 if (c1->regno != c2->regno
306 || INSN_CODE (c1->insn) < 0
307 || INSN_CODE (c1->insn) != INSN_CODE (c2->insn))
308 return false;
309 gcc_assert (c1->nop == c2->nop);
310 for (int i = 0; i < nops; i++)
311 if (i != c1->nop && static_id1->operand[i].type == OP_IN
312 && *id1->operand_loc[i] != *id2->operand_loc[i])
313 return false;
314 return true;
317 /* Insert candidate CAND into the table if it is not there yet.
318 Return candidate which is in the table. */
319 static cand_t
320 insert_cand (cand_t cand)
322 void **entry_ptr;
324 entry_ptr = htab_find_slot (cand_table, cand, INSERT);
325 if (*entry_ptr == NULL)
326 *entry_ptr = (void *) cand;
327 return (cand_t) *entry_ptr;
330 /* Free candidate CAND memory. */
331 static void
332 free_cand (void *cand)
334 free (cand);
337 /* Initiate the candidate table. */
338 static void
339 initiate_cand_table (void)
341 cand_table = htab_create (8000, cand_hash, cand_eq_p,
342 (htab_del) free_cand);
345 /* Finish the candidate table. */
346 static void
347 finish_cand_table (void)
349 htab_delete (cand_table);
354 /* Return true if X contains memory or some UNSPEC. We can not just
355 check insn operands as memory or unspec might be not an operand
356 itself but contain an operand. Insn with memory access is not
357 profitable for rematerialization. Rematerialization of UNSPEC
358 might result in wrong code generation as the UNPEC effect is
359 unknown (e.g. generating a label). */
360 static bool
361 bad_for_rematerialization_p (rtx x)
363 int i, j;
364 const char *fmt;
365 enum rtx_code code;
367 if (MEM_P (x) || GET_CODE (x) == UNSPEC || GET_CODE (x) == UNSPEC_VOLATILE)
368 return true;
369 code = GET_CODE (x);
370 fmt = GET_RTX_FORMAT (code);
371 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
373 if (fmt[i] == 'e')
375 if (bad_for_rematerialization_p (XEXP (x, i)))
376 return true;
378 else if (fmt[i] == 'E')
380 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
381 if (bad_for_rematerialization_p (XVECEXP (x, i, j)))
382 return true;
385 return false;
388 /* If INSN can not be used for rematerialization, return negative
389 value. If INSN can be considered as a candidate for
390 rematerialization, return value which is the operand number of the
391 pseudo for which the insn can be used for rematerialization. Here
392 we consider the insns without any memory, spilled pseudo (except
393 for the rematerialization pseudo), or dying or unused regs. */
394 static int
395 operand_to_remat (rtx_insn *insn)
397 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
398 struct lra_static_insn_data *static_id = id->insn_static_data;
399 struct lra_insn_reg *reg, *found_reg = NULL;
401 /* First find a pseudo which can be rematerialized. */
402 for (reg = id->regs; reg != NULL; reg = reg->next)
403 /* True FRAME_POINTER_NEEDED might be because we can not follow
404 changing sp offsets, e.g. alloca is used. If the insn contains
405 stack pointer in such case, we can not rematerialize it as we
406 can not know sp offset at a rematerialization place. */
407 if (reg->regno == STACK_POINTER_REGNUM && frame_pointer_needed)
408 return -1;
409 else if (reg->type == OP_OUT && ! reg->subreg_p
410 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
412 /* We permits only one spilled reg. */
413 if (found_reg != NULL)
414 return -1;
415 found_reg = reg;
417 if (found_reg == NULL)
418 return -1;
419 if (found_reg->regno < FIRST_PSEUDO_REGISTER)
420 return -1;
421 if (bad_for_rematerialization_p (PATTERN (insn)))
422 return -1;
423 /* Check the other regs are not spilled. */
424 for (reg = id->regs; reg != NULL; reg = reg->next)
425 if (found_reg == reg)
426 continue;
427 else if (reg->type == OP_INOUT)
428 return -1;
429 else if (reg->regno >= FIRST_PSEUDO_REGISTER
430 && reg_renumber[reg->regno] < 0)
431 /* Another spilled reg. */
432 return -1;
433 else if (reg->type == OP_IN)
435 if (find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
436 /* We don't want to make live ranges longer. */
437 return -1;
438 /* Check that there is no output reg as the input one. */
439 for (struct lra_insn_reg *reg2 = id->regs;
440 reg2 != NULL;
441 reg2 = reg2->next)
442 if (reg2->type == OP_OUT && reg->regno == reg2->regno)
443 return -1;
445 /* Find the rematerialization operand. */
446 int nop = static_id->n_operands;
447 for (int i = 0; i < nop; i++)
448 if (REG_P (*id->operand_loc[i])
449 && (int) REGNO (*id->operand_loc[i]) == found_reg->regno)
450 return i;
451 return -1;
454 /* Create candidate for INSN with rematerialization operand NOP and
455 REGNO. Insert the candidate into the table and set up the
456 corresponding INSN_TO_CAND element. */
457 static void
458 create_cand (rtx_insn *insn, int nop, int regno)
460 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
461 rtx reg = *id->operand_loc[nop];
462 gcc_assert (REG_P (reg));
463 int op_regno = REGNO (reg);
464 gcc_assert (op_regno >= FIRST_PSEUDO_REGISTER);
465 cand_t cand = XNEW (struct cand);
466 cand->insn = insn;
467 cand->nop = nop;
468 cand->regno = regno;
469 cand->reload_regno = op_regno == regno ? -1 : op_regno;
470 gcc_assert (cand->regno >= 0);
471 cand_t cand_in_table = insert_cand (cand);
472 insn_to_cand[INSN_UID (insn)] = cand_in_table;
473 if (cand != cand_in_table)
474 free (cand);
475 else
477 /* A new cand. */
478 cand->index = all_cands.length ();
479 all_cands.safe_push (cand);
480 cand->next_regno_cand = regno_cands[cand->regno];
481 regno_cands[cand->regno] = cand;
485 /* Create rematerialization candidates (inserting them into the
486 table). */
487 static void
488 create_cands (void)
490 rtx_insn *insn;
491 struct potential_cand
493 rtx_insn *insn;
494 int nop;
496 struct potential_cand *regno_potential_cand;
498 /* Create candidates. */
499 regno_potential_cand = XCNEWVEC (struct potential_cand, max_reg_num ());
500 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
501 if (INSN_P (insn))
503 rtx set;
504 int src_regno, dst_regno;
505 rtx_insn *insn2;
506 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
507 int nop = operand_to_remat (insn);
508 int regno = -1;
510 if ((set = single_set (insn)) != NULL
511 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
512 && ((src_regno = REGNO (SET_SRC (set)))
513 >= lra_constraint_new_regno_start)
514 && (dst_regno = REGNO (SET_DEST (set))) >= FIRST_PSEUDO_REGISTER
515 && reg_renumber[dst_regno] < 0
516 && (insn2 = regno_potential_cand[src_regno].insn) != NULL
517 && BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn))
518 /* It is an output reload insn after insn can be
519 rematerialized (potential candidate). */
520 create_cand (insn2, regno_potential_cand[src_regno].nop, dst_regno);
521 if (nop < 0)
522 goto fail;
523 gcc_assert (REG_P (*id->operand_loc[nop]));
524 regno = REGNO (*id->operand_loc[nop]);
525 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
526 if (reg_renumber[regno] < 0)
527 create_cand (insn, nop, regno);
528 else
530 regno_potential_cand[regno].insn = insn;
531 regno_potential_cand[regno].nop = nop;
532 goto fail;
534 regno = -1;
535 fail:
536 for (struct lra_insn_reg *reg = id->regs; reg != NULL; reg = reg->next)
537 if (reg->type != OP_IN && reg->regno != regno
538 && reg->regno >= FIRST_PSEUDO_REGISTER)
539 regno_potential_cand[reg->regno].insn = NULL;
541 cands_num = all_cands.length ();
542 free (regno_potential_cand);
547 /* Create and initialize BB data. */
548 static void
549 create_remat_bb_data (void)
551 basic_block bb;
552 remat_bb_data_t bb_info;
554 remat_bb_data = XNEWVEC (struct remat_bb_data,
555 last_basic_block_for_fn (cfun));
556 FOR_ALL_BB_FN (bb, cfun)
558 #ifdef ENABLE_CHECKING
559 if (bb->index < 0 || bb->index >= last_basic_block_for_fn (cfun))
560 abort ();
561 #endif
562 bb_info = get_remat_bb_data (bb);
563 bb_info->bb = bb;
564 bitmap_initialize (&bb_info->changed_regs, &reg_obstack);
565 bitmap_initialize (&bb_info->dead_regs, &reg_obstack);
566 bitmap_initialize (&bb_info->gen_cands, &reg_obstack);
567 bitmap_initialize (&bb_info->livein_cands, &reg_obstack);
568 bitmap_initialize (&bb_info->pavin_cands, &reg_obstack);
569 bitmap_initialize (&bb_info->pavout_cands, &reg_obstack);
570 bitmap_initialize (&bb_info->avin_cands, &reg_obstack);
571 bitmap_initialize (&bb_info->avout_cands, &reg_obstack);
575 /* Dump all candidates to DUMP_FILE. */
576 static void
577 dump_cands (FILE *dump_file)
579 int i;
580 cand_t cand;
582 fprintf (dump_file, "\nCands:\n");
583 for (i = 0; i < (int) cands_num; i++)
585 cand = all_cands[i];
586 fprintf (dump_file, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
587 i, cand->nop, cand->regno, cand->reload_regno);
588 print_inline_rtx (dump_file, cand->insn, 6);
589 fprintf (dump_file, "\n");
593 /* Dump all candidates and BB data. */
594 static void
595 dump_candidates_and_remat_bb_data (void)
597 basic_block bb;
599 if (lra_dump_file == NULL)
600 return;
601 dump_cands (lra_dump_file);
602 FOR_EACH_BB_FN (bb, cfun)
604 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
605 /* Livein */
606 fprintf (lra_dump_file, " register live in:");
607 dump_regset (df_get_live_in (bb), lra_dump_file);
608 putc ('\n', lra_dump_file);
609 /* Liveout */
610 fprintf (lra_dump_file, " register live out:");
611 dump_regset (df_get_live_out (bb), lra_dump_file);
612 putc ('\n', lra_dump_file);
613 /* Changed/dead regs: */
614 fprintf (lra_dump_file, " changed regs:");
615 dump_regset (&get_remat_bb_data (bb)->changed_regs, lra_dump_file);
616 putc ('\n', lra_dump_file);
617 fprintf (lra_dump_file, " dead regs:");
618 dump_regset (&get_remat_bb_data (bb)->dead_regs, lra_dump_file);
619 putc ('\n', lra_dump_file);
620 lra_dump_bitmap_with_title ("cands generated in BB",
621 &get_remat_bb_data (bb)->gen_cands, bb->index);
622 lra_dump_bitmap_with_title ("livein cands in BB",
623 &get_remat_bb_data (bb)->livein_cands, bb->index);
624 lra_dump_bitmap_with_title ("pavin cands in BB",
625 &get_remat_bb_data (bb)->pavin_cands, bb->index);
626 lra_dump_bitmap_with_title ("pavout cands in BB",
627 &get_remat_bb_data (bb)->pavout_cands, bb->index);
628 lra_dump_bitmap_with_title ("avin cands in BB",
629 &get_remat_bb_data (bb)->avin_cands, bb->index);
630 lra_dump_bitmap_with_title ("avout cands in BB",
631 &get_remat_bb_data (bb)->avout_cands, bb->index);
635 /* Free all BB data. */
636 static void
637 finish_remat_bb_data (void)
639 basic_block bb;
641 FOR_EACH_BB_FN (bb, cfun)
643 bitmap_clear (&get_remat_bb_data (bb)->avout_cands);
644 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
645 bitmap_clear (&get_remat_bb_data (bb)->pavout_cands);
646 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
647 bitmap_clear (&get_remat_bb_data (bb)->livein_cands);
648 bitmap_clear (&get_remat_bb_data (bb)->gen_cands);
649 bitmap_clear (&get_remat_bb_data (bb)->dead_regs);
650 bitmap_clear (&get_remat_bb_data (bb)->changed_regs);
652 free (remat_bb_data);
657 /* Update changed_regs and dead_regs of BB from INSN. */
658 static void
659 set_bb_regs (basic_block bb, rtx_insn *insn)
661 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
662 struct lra_insn_reg *reg;
664 for (reg = id->regs; reg != NULL; reg = reg->next)
665 if (reg->type != OP_IN)
666 bitmap_set_bit (&get_remat_bb_data (bb)->changed_regs, reg->regno);
667 else
669 if (find_regno_note (insn, REG_DEAD, (unsigned) reg->regno) != NULL)
670 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs, reg->regno);
672 if (CALL_P (insn))
673 for (int i = 0; i < call_used_regs_arr_len; i++)
674 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs,
675 call_used_regs_arr[i]);
678 /* Calculate changed_regs and dead_regs for each BB. */
679 static void
680 calculate_local_reg_remat_bb_data (void)
682 basic_block bb;
683 rtx_insn *insn;
685 FOR_EACH_BB_FN (bb, cfun)
686 FOR_BB_INSNS (bb, insn)
687 if (INSN_P (insn))
688 set_bb_regs (bb, insn);
693 /* Return true if REGNO is an input operand of INSN. */
694 static bool
695 input_regno_present_p (rtx_insn *insn, int regno)
697 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
698 struct lra_insn_reg *reg;
700 for (reg = id->regs; reg != NULL; reg = reg->next)
701 if (reg->type == OP_IN && reg->regno == regno)
702 return true;
703 return false;
706 /* Return true if a call used register is an input operand of INSN. */
707 static bool
708 call_used_input_regno_present_p (rtx_insn *insn)
710 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
711 struct lra_insn_reg *reg;
713 for (reg = id->regs; reg != NULL; reg = reg->next)
714 if (reg->type == OP_IN && reg->regno <= FIRST_PSEUDO_REGISTER
715 && TEST_HARD_REG_BIT (call_used_reg_set, reg->regno))
716 return true;
717 return false;
720 /* Calculate livein_cands for each BB. */
721 static void
722 calculate_livein_cands (void)
724 basic_block bb;
726 FOR_EACH_BB_FN (bb, cfun)
728 bitmap livein_regs = df_get_live_in (bb);
729 bitmap livein_cands = &get_remat_bb_data (bb)->livein_cands;
730 for (unsigned int i = 0; i < cands_num; i++)
732 cand_t cand = all_cands[i];
733 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
734 struct lra_insn_reg *reg;
736 for (reg = id->regs; reg != NULL; reg = reg->next)
737 if (reg->type == OP_IN && ! bitmap_bit_p (livein_regs, reg->regno))
738 break;
739 if (reg == NULL)
740 bitmap_set_bit (livein_cands, i);
745 /* Calculate gen_cands for each BB. */
746 static void
747 calculate_gen_cands (void)
749 basic_block bb;
750 bitmap gen_cands;
751 bitmap_head gen_insns;
752 rtx_insn *insn;
754 bitmap_initialize (&gen_insns, &reg_obstack);
755 FOR_EACH_BB_FN (bb, cfun)
757 gen_cands = &get_remat_bb_data (bb)->gen_cands;
758 bitmap_clear (&gen_insns);
759 FOR_BB_INSNS (bb, insn)
760 if (INSN_P (insn))
762 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
763 struct lra_insn_reg *reg;
764 unsigned int uid;
765 bitmap_iterator bi;
766 cand_t cand;
767 rtx set;
768 int src_regno = -1, dst_regno = -1;
770 if ((set = single_set (insn)) != NULL
771 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
773 src_regno = REGNO (SET_SRC (set));
774 dst_regno = REGNO (SET_DEST (set));
777 /* Update gen_cands: */
778 bitmap_clear (&temp_bitmap);
779 for (reg = id->regs; reg != NULL; reg = reg->next)
780 if (reg->type != OP_IN
781 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
782 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
784 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
786 cand = insn_to_cand[INSN_UID (insn2)];
787 gcc_assert (cand != NULL);
788 /* Ignore the reload insn. */
789 if (src_regno == cand->reload_regno
790 && dst_regno == cand->regno)
791 continue;
792 if (cand->regno == reg->regno
793 || input_regno_present_p (insn2, reg->regno))
795 bitmap_clear_bit (gen_cands, cand->index);
796 bitmap_set_bit (&temp_bitmap, uid);
800 if (CALL_P (insn))
801 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
803 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
805 cand = insn_to_cand[INSN_UID (insn2)];
806 gcc_assert (cand != NULL);
807 if (call_used_input_regno_present_p (insn2))
809 bitmap_clear_bit (gen_cands, cand->index);
810 bitmap_set_bit (&temp_bitmap, uid);
813 bitmap_and_compl_into (&gen_insns, &temp_bitmap);
815 cand = insn_to_cand[INSN_UID (insn)];
816 if (cand != NULL)
818 bitmap_set_bit (gen_cands, cand->index);
819 bitmap_set_bit (&gen_insns, INSN_UID (insn));
823 bitmap_clear (&gen_insns);
828 /* The common transfer function used by the DF equation solver to
829 propagate (partial) availability info BB_IN to BB_OUT through block
830 with BB_INDEX according to the following equation:
832 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
834 static bool
835 cand_trans_fun (int bb_index, bitmap bb_in, bitmap bb_out)
837 remat_bb_data_t bb_info;
838 bitmap bb_livein, bb_changed_regs, bb_dead_regs;
839 unsigned int cid;
840 bitmap_iterator bi;
842 bb_info = get_remat_bb_data_by_index (bb_index);
843 bb_livein = &bb_info->livein_cands;
844 bb_changed_regs = &bb_info->changed_regs;
845 bb_dead_regs = &bb_info->dead_regs;
846 /* Calculate killed avin cands -- cands whose regs are changed or
847 becoming dead in the BB. We calculate it here as we hope that
848 repeated calculations are compensated by smaller size of BB_IN in
849 comparison with all candidates number. */
850 bitmap_clear (&temp_bitmap);
851 EXECUTE_IF_SET_IN_BITMAP (bb_in, 0, cid, bi)
853 cand_t cand = all_cands[cid];
854 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
855 struct lra_insn_reg *reg;
857 if (! bitmap_bit_p (bb_livein, cid))
859 bitmap_set_bit (&temp_bitmap, cid);
860 continue;
862 for (reg = id->regs; reg != NULL; reg = reg->next)
863 /* Ignore all outputs which are not the regno for
864 rematerialization. */
865 if (reg->type == OP_OUT && reg->regno != cand->regno)
866 continue;
867 else if (bitmap_bit_p (bb_changed_regs, reg->regno)
868 || bitmap_bit_p (bb_dead_regs, reg->regno))
870 bitmap_set_bit (&temp_bitmap, cid);
871 break;
873 /* Check regno for rematerialization. */
874 if (bitmap_bit_p (bb_changed_regs, cand->regno)
875 || bitmap_bit_p (bb_dead_regs, cand->regno))
876 bitmap_set_bit (&temp_bitmap, cid);
878 return bitmap_ior_and_compl (bb_out,
879 &bb_info->gen_cands, bb_in, &temp_bitmap);
884 /* The transfer function used by the DF equation solver to propagate
885 partial candidate availability info through block with BB_INDEX
886 according to the following equation:
888 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
890 static bool
891 cand_pav_trans_fun (int bb_index)
893 remat_bb_data_t bb_info;
895 bb_info = get_remat_bb_data_by_index (bb_index);
896 return cand_trans_fun (bb_index, &bb_info->pavin_cands,
897 &bb_info->pavout_cands);
900 /* The confluence function used by the DF equation solver to set up
901 cand_pav info for a block BB without predecessor. */
902 static void
903 cand_pav_con_fun_0 (basic_block bb)
905 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
908 /* The confluence function used by the DF equation solver to propagate
909 partial candidate availability info from predecessor to successor
910 on edge E (pred->bb) according to the following equation:
912 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
914 static bool
915 cand_pav_con_fun_n (edge e)
917 basic_block pred = e->src;
918 basic_block bb = e->dest;
919 remat_bb_data_t bb_info;
920 bitmap bb_pavin, pred_pavout;
922 bb_info = get_remat_bb_data (bb);
923 bb_pavin = &bb_info->pavin_cands;
924 pred_pavout = &get_remat_bb_data (pred)->pavout_cands;
925 return bitmap_ior_into (bb_pavin, pred_pavout);
930 /* The transfer function used by the DF equation solver to propagate
931 candidate availability info through block with BB_INDEX according
932 to the following equation:
934 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
936 static bool
937 cand_av_trans_fun (int bb_index)
939 remat_bb_data_t bb_info;
941 bb_info = get_remat_bb_data_by_index (bb_index);
942 return cand_trans_fun (bb_index, &bb_info->avin_cands,
943 &bb_info->avout_cands);
946 /* The confluence function used by the DF equation solver to set up
947 cand_av info for a block BB without predecessor. */
948 static void
949 cand_av_con_fun_0 (basic_block bb)
951 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
954 /* The confluence function used by the DF equation solver to propagate
955 cand_av info from predecessor to successor on edge E (pred->bb)
956 according to the following equation:
958 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
960 static bool
961 cand_av_con_fun_n (edge e)
963 basic_block pred = e->src;
964 basic_block bb = e->dest;
965 remat_bb_data_t bb_info;
966 bitmap bb_avin, pred_avout;
968 bb_info = get_remat_bb_data (bb);
969 bb_avin = &bb_info->avin_cands;
970 pred_avout = &get_remat_bb_data (pred)->avout_cands;
971 return bitmap_and_into (bb_avin, pred_avout);
974 /* Calculate available candidates for each BB. */
975 static void
976 calculate_global_remat_bb_data (void)
978 basic_block bb;
980 df_simple_dataflow
981 (DF_FORWARD, NULL, cand_pav_con_fun_0, cand_pav_con_fun_n,
982 cand_pav_trans_fun, &all_blocks,
983 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
984 /* Initialize avin by pavin. */
985 FOR_EACH_BB_FN (bb, cfun)
986 bitmap_copy (&get_remat_bb_data (bb)->avin_cands,
987 &get_remat_bb_data (bb)->pavin_cands);
988 df_simple_dataflow
989 (DF_FORWARD, NULL, cand_av_con_fun_0, cand_av_con_fun_n,
990 cand_av_trans_fun, &all_blocks,
991 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
996 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
997 static void
998 change_sp_offset (rtx_insn *insns, HOST_WIDE_INT sp_offset)
1000 for (rtx_insn *insn = insns; insn != NULL; insn = NEXT_INSN (insn))
1001 eliminate_regs_in_insn (insn, false, false, sp_offset);
1004 /* Return start hard register of REG (can be a hard or a pseudo reg)
1005 or -1 (if it is a spilled pseudo). Return number of hard registers
1006 occupied by REG through parameter NREGS if the start hard reg is
1007 not negative. */
1008 static int
1009 get_hard_regs (struct lra_insn_reg *reg, int &nregs)
1011 int regno = reg->regno;
1012 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
1014 if (hard_regno >= 0)
1015 nregs = hard_regno_nregs[hard_regno][reg->biggest_mode];
1016 return hard_regno;
1019 /* Insert rematerialization insns using the data-flow data calculated
1020 earlier. */
1021 static bool
1022 do_remat (void)
1024 rtx_insn *insn;
1025 basic_block bb;
1026 bitmap_head avail_cands;
1027 bool changed_p = false;
1028 /* Living hard regs and hard registers of living pseudos. */
1029 HARD_REG_SET live_hard_regs;
1031 bitmap_initialize (&avail_cands, &reg_obstack);
1032 FOR_EACH_BB_FN (bb, cfun)
1034 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (bb));
1035 bitmap_and (&avail_cands, &get_remat_bb_data (bb)->avin_cands,
1036 &get_remat_bb_data (bb)->livein_cands);
1037 FOR_BB_INSNS (bb, insn)
1039 if (!NONDEBUG_INSN_P (insn))
1040 continue;
1042 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1043 struct lra_static_insn_data *static_id = id->insn_static_data;
1044 struct lra_insn_reg *reg;
1045 cand_t cand;
1046 unsigned int cid;
1047 bitmap_iterator bi;
1048 rtx set;
1049 int src_regno = -1, dst_regno = -1;
1051 if ((set = single_set (insn)) != NULL
1052 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
1054 src_regno = REGNO (SET_SRC (set));
1055 dst_regno = REGNO (SET_DEST (set));
1058 cand = NULL;
1059 /* Check possibility of rematerialization (hard reg or
1060 unpsilled pseudo <- spilled pseudo): */
1061 if (dst_regno >= 0 && src_regno >= FIRST_PSEUDO_REGISTER
1062 && reg_renumber[src_regno] < 0
1063 && (dst_regno < FIRST_PSEUDO_REGISTER
1064 || reg_renumber[dst_regno] >= 0))
1066 for (cand = regno_cands[src_regno];
1067 cand != NULL;
1068 cand = cand->next_regno_cand)
1069 if (bitmap_bit_p (&avail_cands, cand->index))
1070 break;
1072 int i, hard_regno, nregs;
1073 rtx_insn *remat_insn = NULL;
1074 HOST_WIDE_INT cand_sp_offset = 0;
1075 if (cand != NULL)
1077 lra_insn_recog_data_t cand_id
1078 = lra_get_insn_recog_data (cand->insn);
1079 struct lra_static_insn_data *static_cand_id
1080 = cand_id->insn_static_data;
1081 rtx saved_op = *cand_id->operand_loc[cand->nop];
1083 /* Check clobbers do not kill something living. */
1084 gcc_assert (REG_P (saved_op));
1085 int ignore_regno = REGNO (saved_op);
1087 for (reg = cand_id->regs; reg != NULL; reg = reg->next)
1088 if (reg->type != OP_IN && reg->regno != ignore_regno)
1090 hard_regno = get_hard_regs (reg, nregs);
1091 gcc_assert (hard_regno >= 0);
1092 for (i = 0; i < nregs; i++)
1093 if (TEST_HARD_REG_BIT (live_hard_regs, hard_regno + i))
1094 break;
1095 if (i < nregs)
1096 break;
1099 if (reg == NULL)
1101 for (reg = static_cand_id->hard_regs;
1102 reg != NULL;
1103 reg = reg->next)
1104 if (reg->type != OP_IN
1105 && TEST_HARD_REG_BIT (live_hard_regs, reg->regno))
1106 break;
1109 if (reg == NULL)
1111 *cand_id->operand_loc[cand->nop] = SET_DEST (set);
1112 lra_update_insn_regno_info (cand->insn);
1113 bool ok_p = lra_constrain_insn (cand->insn);
1114 if (ok_p)
1116 rtx remat_pat = copy_insn (PATTERN (cand->insn));
1118 start_sequence ();
1119 emit_insn (remat_pat);
1120 remat_insn = get_insns ();
1121 end_sequence ();
1122 if (recog_memoized (remat_insn) < 0)
1123 remat_insn = NULL;
1124 cand_sp_offset = cand_id->sp_offset;
1126 *cand_id->operand_loc[cand->nop] = saved_op;
1127 lra_update_insn_regno_info (cand->insn);
1131 bitmap_clear (&temp_bitmap);
1132 /* Update avail_cands (see analogous code for
1133 calculate_gen_cands). */
1134 for (reg = id->regs; reg != NULL; reg = reg->next)
1135 if (reg->type != OP_IN
1136 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1137 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1139 cand = all_cands[cid];
1141 /* Ignore the reload insn. */
1142 if (src_regno == cand->reload_regno
1143 && dst_regno == cand->regno)
1144 continue;
1145 if (cand->regno == reg->regno
1146 || input_regno_present_p (cand->insn, reg->regno))
1147 bitmap_set_bit (&temp_bitmap, cand->index);
1150 if (CALL_P (insn))
1151 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1153 cand = all_cands[cid];
1155 if (call_used_input_regno_present_p (cand->insn))
1156 bitmap_set_bit (&temp_bitmap, cand->index);
1159 bitmap_and_compl_into (&avail_cands, &temp_bitmap);
1160 if ((cand = insn_to_cand[INSN_UID (insn)]) != NULL)
1161 bitmap_set_bit (&avail_cands, cand->index);
1163 if (remat_insn != NULL)
1165 HOST_WIDE_INT sp_offset_change = cand_sp_offset - id->sp_offset;
1166 if (sp_offset_change != 0)
1167 change_sp_offset (remat_insn, sp_offset_change);
1168 lra_process_new_insns (insn, remat_insn, NULL,
1169 "Inserting rematerialization insn");
1170 lra_set_insn_deleted (insn);
1171 changed_p = true;
1172 continue;
1175 /* Update live hard regs: */
1176 for (reg = id->regs; reg != NULL; reg = reg->next)
1177 if (reg->type == OP_IN
1178 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1180 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1181 continue;
1182 for (i = 0; i < nregs; i++)
1183 CLEAR_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1185 else if (reg->type != OP_IN
1186 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1188 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1189 continue;
1190 for (i = 0; i < nregs; i++)
1191 SET_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1193 /* Process also hard regs (e.g. CC register) which are part
1194 of insn definition. */
1195 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1196 if (reg->type == OP_IN
1197 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1198 CLEAR_HARD_REG_BIT (live_hard_regs, reg->regno);
1199 else if (reg->type != OP_IN
1200 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1201 SET_HARD_REG_BIT (live_hard_regs, reg->regno);
1204 bitmap_clear (&avail_cands);
1205 return changed_p;
1210 /* Entry point of the rematerialization sub-pass. Return true if we
1211 did any rematerialization. */
1212 bool
1213 lra_remat (void)
1215 basic_block bb;
1216 bool result;
1217 int max_regno = max_reg_num ();
1219 if (! flag_lra_remat)
1220 return false;
1221 timevar_push (TV_LRA_REMAT);
1222 insn_to_cand = XCNEWVEC (cand_t, get_max_uid ());
1223 regno_cands = XCNEWVEC (cand_t, max_regno);
1224 all_cands.create (8000);
1225 call_used_regs_arr_len = 0;
1226 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1227 if (call_used_regs[i])
1228 call_used_regs_arr[call_used_regs_arr_len++] = i;
1229 initiate_cand_table ();
1230 create_cands ();
1231 create_remat_bb_data ();
1232 bitmap_initialize (&temp_bitmap, &reg_obstack);
1233 calculate_local_reg_remat_bb_data ();
1234 calculate_livein_cands ();
1235 calculate_gen_cands ();
1236 bitmap_initialize (&all_blocks, &reg_obstack);
1237 FOR_ALL_BB_FN (bb, cfun)
1238 bitmap_set_bit (&all_blocks, bb->index);
1239 calculate_global_remat_bb_data ();
1240 dump_candidates_and_remat_bb_data ();
1241 result = do_remat ();
1242 all_cands.release ();
1243 bitmap_clear (&temp_bitmap);
1244 finish_remat_bb_data ();
1245 finish_cand_table ();
1246 bitmap_clear (&all_blocks);
1247 free (regno_cands);
1248 free (insn_to_cand);
1249 timevar_pop (TV_LRA_REMAT);
1250 return result;