]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lra-remat.c
Fix latent LRA remat issue (PR68730)
[thirdparty/gcc.git] / gcc / lra-remat.c
1 /* Rematerialize pseudos values.
2 Copyright (C) 2014-2016 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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.
26
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.
32
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.
37
38 The rematerialization sub-pass could be improved further in the
39 following ways:
40
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. */
53
54
55 #include "config.h"
56 #include "system.h"
57 #include "coretypes.h"
58 #include "backend.h"
59 #include "rtl.h"
60 #include "df.h"
61 #include "insn-config.h"
62 #include "regs.h"
63 #include "ira.h"
64 #include "recog.h"
65 #include "lra.h"
66 #include "lra-int.h"
67
68 /* Number of candidates for rematerialization. */
69 static unsigned int cands_num;
70
71 /* The following is used for representation of call_used_reg_set in
72 form array whose elements are hard register numbers with nonzero bit
73 in CALL_USED_REG_SET. */
74 static int call_used_regs_arr_len;
75 static int call_used_regs_arr[FIRST_PSEUDO_REGISTER];
76
77 /* Bitmap used for different calculations. */
78 static bitmap_head temp_bitmap;
79
80 /* Registers accessed via subreg_p. */
81 static bitmap_head subreg_regs;
82
83 typedef struct cand *cand_t;
84 typedef const struct cand *const_cand_t;
85
86 /* Insn candidates for rematerialization. The candidate insn should
87 have the following properies:
88 o no any memory (as access to memory is non-profitable)
89 o no INOUT regs (it means no non-paradoxical subreg of output reg)
90 o one output spilled pseudo (or reload pseudo of a spilled pseudo)
91 o all other pseudos are with assigned hard regs. */
92 struct cand
93 {
94 /* Index of the candidates in all_cands. */
95 int index;
96 /* The candidate insn. */
97 rtx_insn *insn;
98 /* Insn pseudo regno for rematerialization. */
99 int regno;
100 /* Non-negative if a reload pseudo is in the insn instead of the
101 pseudo for rematerialization. */
102 int reload_regno;
103 /* Number of the operand containing the regno or its reload
104 regno. */
105 int nop;
106 /* Next candidate for the same regno. */
107 cand_t next_regno_cand;
108 };
109
110 /* Vector containing all candidates. */
111 static vec<cand_t> all_cands;
112 /* Map: insn -> candidate representing it. It is null if the insn can
113 not be used for rematerialization. */
114 static cand_t *insn_to_cand;
115 /* A secondary map, for candidates that involve two insns, where the
116 second one makes the equivalence. The candidate must not be used
117 before seeing this activation insn. */
118 static cand_t *insn_to_cand_activation;
119
120 /* Map regno -> candidates can be used for the regno
121 rematerialization. */
122 static cand_t *regno_cands;
123
124 /* Data about basic blocks used for the rematerialization
125 sub-pass. */
126 struct remat_bb_data
127 {
128 /* Basic block about which the below data are. */
129 basic_block bb;
130 /* Registers changed in the basic block: */
131 bitmap_head changed_regs;
132 /* Registers becoming dead in the BB. */
133 bitmap_head dead_regs;
134 /* Cands present in the BB whose in/out regs are not changed after
135 the cands occurence and are not dead (except the reload
136 regno). */
137 bitmap_head gen_cands;
138 bitmap_head livein_cands; /* cands whose inputs live at the BB start. */
139 bitmap_head pavin_cands; /* cands partially available at BB entry. */
140 bitmap_head pavout_cands; /* cands partially available at BB exit. */
141 bitmap_head avin_cands; /* cands available at the entry of the BB. */
142 bitmap_head avout_cands; /* cands available at the exit of the BB. */
143 };
144
145 /* Array for all BB data. Indexed by the corresponding BB index. */
146 typedef struct remat_bb_data *remat_bb_data_t;
147
148 /* Basic blocks for data flow problems -- all bocks except the special
149 ones. */
150 static bitmap_head all_blocks;
151
152 /* All basic block data are referred through the following array. */
153 static remat_bb_data_t remat_bb_data;
154
155 /* Two small functions for access to the bb data. */
156 static inline remat_bb_data_t
157 get_remat_bb_data (basic_block bb)
158 {
159 return &remat_bb_data[(bb)->index];
160 }
161
162 static inline remat_bb_data_t
163 get_remat_bb_data_by_index (int index)
164 {
165 return &remat_bb_data[index];
166 }
167
168 \f
169
170 /* Recursive hash function for RTL X. */
171 static hashval_t
172 rtx_hash (rtx x)
173 {
174 int i, j;
175 enum rtx_code code;
176 const char *fmt;
177 hashval_t val = 0;
178
179 if (x == 0)
180 return val;
181
182 code = GET_CODE (x);
183 val += (int) code + 4095;
184
185 /* Some RTL can be compared nonrecursively. */
186 switch (code)
187 {
188 case REG:
189 return val + REGNO (x);
190
191 case LABEL_REF:
192 return iterative_hash_object (XEXP (x, 0), val);
193
194 case SYMBOL_REF:
195 return iterative_hash_object (XSTR (x, 0), val);
196
197 case SCRATCH:
198 case CONST_DOUBLE:
199 case CONST_INT:
200 case CONST_VECTOR:
201 return val;
202
203 default:
204 break;
205 }
206
207 /* Hash the elements. */
208 fmt = GET_RTX_FORMAT (code);
209 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
210 {
211 switch (fmt[i])
212 {
213 case 'w':
214 val += XWINT (x, i);
215 break;
216
217 case 'n':
218 case 'i':
219 val += XINT (x, i);
220 break;
221
222 case 'V':
223 case 'E':
224 val += XVECLEN (x, i);
225
226 for (j = 0; j < XVECLEN (x, i); j++)
227 val += rtx_hash (XVECEXP (x, i, j));
228 break;
229
230 case 'e':
231 val += rtx_hash (XEXP (x, i));
232 break;
233
234 case 'S':
235 case 's':
236 val += htab_hash_string (XSTR (x, i));
237 break;
238
239 case 'u':
240 case '0':
241 case 't':
242 break;
243
244 /* It is believed that rtx's at this level will never
245 contain anything but integers and other rtx's, except for
246 within LABEL_REFs and SYMBOL_REFs. */
247 default:
248 abort ();
249 }
250 }
251 return val;
252 }
253
254 \f
255
256 /* Hash table for the candidates. Different insns (e.g. structurally
257 the same insns or even insns with different unused output regs) can
258 be represented by the same candidate in the table. */
259 static htab_t cand_table;
260
261 /* Hash function for candidate CAND. */
262 static hashval_t
263 cand_hash (const void *cand)
264 {
265 const_cand_t c = (const_cand_t) cand;
266 lra_insn_recog_data_t id = lra_get_insn_recog_data (c->insn);
267 struct lra_static_insn_data *static_id = id->insn_static_data;
268 int nops = static_id->n_operands;
269 hashval_t hash = 0;
270
271 for (int i = 0; i < nops; i++)
272 if (i == c->nop)
273 hash = iterative_hash_object (c->regno, hash);
274 else if (static_id->operand[i].type == OP_IN)
275 hash = iterative_hash_object (*id->operand_loc[i], hash);
276 return hash;
277 }
278
279 /* Equal function for candidates CAND1 and CAND2. They are equal if
280 the corresponding candidate insns have the same code, the same
281 regno for rematerialization, the same input operands. */
282 static int
283 cand_eq_p (const void *cand1, const void *cand2)
284 {
285 const_cand_t c1 = (const_cand_t) cand1;
286 const_cand_t c2 = (const_cand_t) cand2;
287 lra_insn_recog_data_t id1 = lra_get_insn_recog_data (c1->insn);
288 lra_insn_recog_data_t id2 = lra_get_insn_recog_data (c2->insn);
289 struct lra_static_insn_data *static_id1 = id1->insn_static_data;
290 int nops = static_id1->n_operands;
291
292 if (c1->regno != c2->regno
293 || INSN_CODE (c1->insn) < 0
294 || INSN_CODE (c1->insn) != INSN_CODE (c2->insn))
295 return false;
296 gcc_assert (c1->nop == c2->nop);
297 for (int i = 0; i < nops; i++)
298 if (i != c1->nop && static_id1->operand[i].type == OP_IN
299 && *id1->operand_loc[i] != *id2->operand_loc[i])
300 return false;
301 return true;
302 }
303
304 /* Insert candidate CAND into the table if it is not there yet.
305 Return candidate which is in the table. */
306 static cand_t
307 insert_cand (cand_t cand)
308 {
309 void **entry_ptr;
310
311 entry_ptr = htab_find_slot (cand_table, cand, INSERT);
312 if (*entry_ptr == NULL)
313 *entry_ptr = (void *) cand;
314 return (cand_t) *entry_ptr;
315 }
316
317 /* Free candidate CAND memory. */
318 static void
319 free_cand (void *cand)
320 {
321 free (cand);
322 }
323
324 /* Initiate the candidate table. */
325 static void
326 initiate_cand_table (void)
327 {
328 cand_table = htab_create (8000, cand_hash, cand_eq_p,
329 (htab_del) free_cand);
330 }
331
332 /* Finish the candidate table. */
333 static void
334 finish_cand_table (void)
335 {
336 htab_delete (cand_table);
337 }
338
339 \f
340
341 /* Return true if X contains memory or some UNSPEC. We can not just
342 check insn operands as memory or unspec might be not an operand
343 itself but contain an operand. Insn with memory access is not
344 profitable for rematerialization. Rematerialization of UNSPEC
345 might result in wrong code generation as the UNPEC effect is
346 unknown (e.g. generating a label). */
347 static bool
348 bad_for_rematerialization_p (rtx x)
349 {
350 int i, j;
351 const char *fmt;
352 enum rtx_code code;
353
354 if (MEM_P (x) || GET_CODE (x) == UNSPEC || GET_CODE (x) == UNSPEC_VOLATILE)
355 return true;
356 code = GET_CODE (x);
357 fmt = GET_RTX_FORMAT (code);
358 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
359 {
360 if (fmt[i] == 'e')
361 {
362 if (bad_for_rematerialization_p (XEXP (x, i)))
363 return true;
364 }
365 else if (fmt[i] == 'E')
366 {
367 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
368 if (bad_for_rematerialization_p (XVECEXP (x, i, j)))
369 return true;
370 }
371 }
372 return false;
373 }
374
375 /* If INSN can not be used for rematerialization, return negative
376 value. If INSN can be considered as a candidate for
377 rematerialization, return value which is the operand number of the
378 pseudo for which the insn can be used for rematerialization. Here
379 we consider the insns without any memory, spilled pseudo (except
380 for the rematerialization pseudo), or dying or unused regs. */
381 static int
382 operand_to_remat (rtx_insn *insn)
383 {
384 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
385 struct lra_static_insn_data *static_id = id->insn_static_data;
386 struct lra_insn_reg *reg, *found_reg = NULL;
387
388 /* Don't rematerialize insns which can change PC. */
389 if (JUMP_P (insn) || CALL_P (insn))
390 return -1;
391 /* First find a pseudo which can be rematerialized. */
392 for (reg = id->regs; reg != NULL; reg = reg->next)
393 {
394 /* True FRAME_POINTER_NEEDED might be because we can not follow
395 changing sp offsets, e.g. alloca is used. If the insn contains
396 stack pointer in such case, we can not rematerialize it as we
397 can not know sp offset at a rematerialization place. */
398 if (reg->regno == STACK_POINTER_REGNUM && frame_pointer_needed)
399 return -1;
400 else if (reg->type == OP_OUT && ! reg->subreg_p
401 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
402 {
403 /* We permits only one spilled reg. */
404 if (found_reg != NULL)
405 return -1;
406 found_reg = reg;
407 }
408 /* IRA calculates conflicts separately for subregs of two words
409 pseudo. Even if the pseudo lives, e.g. one its subreg can be
410 used lately, another subreg hard register can be already used
411 for something else. In such case, it is not safe to
412 rematerialize the insn. */
413 if (reg->regno >= FIRST_PSEUDO_REGISTER
414 && bitmap_bit_p (&subreg_regs, reg->regno))
415 return -1;
416 }
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)
434 {
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;
444 if (reg->regno < FIRST_PSEUDO_REGISTER)
445 for (struct lra_insn_reg *reg2 = static_id->hard_regs;
446 reg2 != NULL;
447 reg2 = reg2->next)
448 if (reg2->type == OP_OUT
449 && reg->regno <= reg2->regno
450 && (reg2->regno
451 < (reg->regno
452 + hard_regno_nregs[reg->regno][reg->biggest_mode])))
453 return -1;
454 }
455 /* Find the rematerialization operand. */
456 int nop = static_id->n_operands;
457 for (int i = 0; i < nop; i++)
458 if (REG_P (*id->operand_loc[i])
459 && (int) REGNO (*id->operand_loc[i]) == found_reg->regno)
460 return i;
461 return -1;
462 }
463
464 /* Create candidate for INSN with rematerialization operand NOP and
465 REGNO. Insert the candidate into the table and set up the
466 corresponding INSN_TO_CAND element. */
467 static void
468 create_cand (rtx_insn *insn, int nop, int regno, rtx_insn *activation = NULL)
469 {
470 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
471 rtx reg = *id->operand_loc[nop];
472 gcc_assert (REG_P (reg));
473 int op_regno = REGNO (reg);
474 gcc_assert (op_regno >= FIRST_PSEUDO_REGISTER);
475 cand_t cand = XNEW (struct cand);
476 cand->insn = insn;
477 cand->nop = nop;
478 cand->regno = regno;
479 cand->reload_regno = op_regno == regno ? -1 : op_regno;
480 gcc_assert (cand->regno >= 0);
481 cand_t cand_in_table = insert_cand (cand);
482 insn_to_cand[INSN_UID (insn)] = cand_in_table;
483 if (cand != cand_in_table)
484 free (cand);
485 else
486 {
487 /* A new cand. */
488 cand->index = all_cands.length ();
489 all_cands.safe_push (cand);
490 cand->next_regno_cand = regno_cands[cand->regno];
491 regno_cands[cand->regno] = cand;
492 }
493 if (activation)
494 insn_to_cand_activation[INSN_UID (activation)] = cand_in_table;
495 }
496
497 /* Create rematerialization candidates (inserting them into the
498 table). */
499 static void
500 create_cands (void)
501 {
502 rtx_insn *insn;
503 struct potential_cand
504 {
505 rtx_insn *insn;
506 int nop;
507 };
508 struct potential_cand *regno_potential_cand;
509
510 /* Create candidates. */
511 regno_potential_cand = XCNEWVEC (struct potential_cand, max_reg_num ());
512 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
513 if (NONDEBUG_INSN_P (insn))
514 {
515 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
516 int keep_regno = -1;
517 rtx set = single_set (insn);
518 int nop;
519
520 /* See if this is an output reload for a previous insn. */
521 if (set != NULL
522 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
523 {
524 rtx dstreg = SET_DEST (set);
525 int src_regno = REGNO (SET_SRC (set));
526 int dst_regno = REGNO (dstreg);
527 rtx_insn *insn2 = regno_potential_cand[src_regno].insn;
528
529 if (insn2 != NULL
530 && dst_regno >= FIRST_PSEUDO_REGISTER
531 && reg_renumber[dst_regno] < 0
532 && BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn))
533 {
534 create_cand (insn2, regno_potential_cand[src_regno].nop,
535 dst_regno, insn);
536 goto done;
537 }
538 }
539
540 nop = operand_to_remat (insn);
541 if (nop >= 0)
542 {
543 gcc_assert (REG_P (*id->operand_loc[nop]));
544 int regno = REGNO (*id->operand_loc[nop]);
545 gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
546 /* If we're setting an unrenumbered pseudo, make a candidate immediately.
547 If it's an output reload register, save it for later; the code above
548 looks for output reload insns later on. */
549 if (reg_renumber[regno] < 0)
550 create_cand (insn, nop, regno);
551 else if (regno >= lra_constraint_new_regno_start)
552 {
553 regno_potential_cand[regno].insn = insn;
554 regno_potential_cand[regno].nop = nop;
555 keep_regno = regno;
556 }
557 }
558
559 done:
560 for (struct lra_insn_reg *reg = id->regs; reg != NULL; reg = reg->next)
561 if (reg->type != OP_IN && reg->regno != keep_regno
562 && reg->regno >= FIRST_PSEUDO_REGISTER)
563 regno_potential_cand[reg->regno].insn = NULL;
564 }
565 cands_num = all_cands.length ();
566 free (regno_potential_cand);
567 }
568
569 \f
570
571 /* Create and initialize BB data. */
572 static void
573 create_remat_bb_data (void)
574 {
575 basic_block bb;
576 remat_bb_data_t bb_info;
577
578 remat_bb_data = XNEWVEC (struct remat_bb_data,
579 last_basic_block_for_fn (cfun));
580 FOR_ALL_BB_FN (bb, cfun)
581 {
582 gcc_checking_assert (bb->index >= 0
583 && bb->index < last_basic_block_for_fn (cfun));
584 bb_info = get_remat_bb_data (bb);
585 bb_info->bb = bb;
586 bitmap_initialize (&bb_info->changed_regs, &reg_obstack);
587 bitmap_initialize (&bb_info->dead_regs, &reg_obstack);
588 bitmap_initialize (&bb_info->gen_cands, &reg_obstack);
589 bitmap_initialize (&bb_info->livein_cands, &reg_obstack);
590 bitmap_initialize (&bb_info->pavin_cands, &reg_obstack);
591 bitmap_initialize (&bb_info->pavout_cands, &reg_obstack);
592 bitmap_initialize (&bb_info->avin_cands, &reg_obstack);
593 bitmap_initialize (&bb_info->avout_cands, &reg_obstack);
594 }
595 }
596
597 /* Dump all candidates to DUMP_FILE. */
598 static void
599 dump_cands (FILE *dump_file)
600 {
601 int i;
602 cand_t cand;
603
604 fprintf (dump_file, "\nCands:\n");
605 for (i = 0; i < (int) cands_num; i++)
606 {
607 cand = all_cands[i];
608 fprintf (dump_file, "%d (nop=%d, remat_regno=%d, reload_regno=%d):\n",
609 i, cand->nop, cand->regno, cand->reload_regno);
610 print_inline_rtx (dump_file, cand->insn, 6);
611 fprintf (dump_file, "\n");
612 }
613 }
614
615 /* Dump all candidates and BB data. */
616 static void
617 dump_candidates_and_remat_bb_data (void)
618 {
619 basic_block bb;
620
621 if (lra_dump_file == NULL)
622 return;
623 dump_cands (lra_dump_file);
624 FOR_EACH_BB_FN (bb, cfun)
625 {
626 fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
627 /* Livein */
628 fprintf (lra_dump_file, " register live in:");
629 dump_regset (df_get_live_in (bb), lra_dump_file);
630 putc ('\n', lra_dump_file);
631 /* Liveout */
632 fprintf (lra_dump_file, " register live out:");
633 dump_regset (df_get_live_out (bb), lra_dump_file);
634 putc ('\n', lra_dump_file);
635 /* Changed/dead regs: */
636 fprintf (lra_dump_file, " changed regs:");
637 dump_regset (&get_remat_bb_data (bb)->changed_regs, lra_dump_file);
638 putc ('\n', lra_dump_file);
639 fprintf (lra_dump_file, " dead regs:");
640 dump_regset (&get_remat_bb_data (bb)->dead_regs, lra_dump_file);
641 putc ('\n', lra_dump_file);
642 lra_dump_bitmap_with_title ("cands generated in BB",
643 &get_remat_bb_data (bb)->gen_cands, bb->index);
644 lra_dump_bitmap_with_title ("livein cands in BB",
645 &get_remat_bb_data (bb)->livein_cands, bb->index);
646 lra_dump_bitmap_with_title ("pavin cands in BB",
647 &get_remat_bb_data (bb)->pavin_cands, bb->index);
648 lra_dump_bitmap_with_title ("pavout cands in BB",
649 &get_remat_bb_data (bb)->pavout_cands, bb->index);
650 lra_dump_bitmap_with_title ("avin cands in BB",
651 &get_remat_bb_data (bb)->avin_cands, bb->index);
652 lra_dump_bitmap_with_title ("avout cands in BB",
653 &get_remat_bb_data (bb)->avout_cands, bb->index);
654 }
655 fprintf (lra_dump_file, "subreg regs:");
656 dump_regset (&subreg_regs, lra_dump_file);
657 putc ('\n', lra_dump_file);
658 }
659
660 /* Free all BB data. */
661 static void
662 finish_remat_bb_data (void)
663 {
664 basic_block bb;
665
666 FOR_EACH_BB_FN (bb, cfun)
667 {
668 bitmap_clear (&get_remat_bb_data (bb)->avout_cands);
669 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
670 bitmap_clear (&get_remat_bb_data (bb)->pavout_cands);
671 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
672 bitmap_clear (&get_remat_bb_data (bb)->livein_cands);
673 bitmap_clear (&get_remat_bb_data (bb)->gen_cands);
674 bitmap_clear (&get_remat_bb_data (bb)->dead_regs);
675 bitmap_clear (&get_remat_bb_data (bb)->changed_regs);
676 }
677 free (remat_bb_data);
678 }
679
680 \f
681
682 /* Update changed_regs, dead_regs, subreg_regs of BB from INSN. */
683 static void
684 set_bb_regs (basic_block bb, rtx_insn *insn)
685 {
686 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
687 remat_bb_data_t bb_info = get_remat_bb_data (bb);
688 struct lra_insn_reg *reg;
689
690 for (reg = id->regs; reg != NULL; reg = reg->next)
691 {
692 unsigned regno = reg->regno;
693 if (reg->type != OP_IN)
694 bitmap_set_bit (&bb_info->changed_regs, regno);
695 else if (find_regno_note (insn, REG_DEAD, regno) != NULL)
696 bitmap_set_bit (&bb_info->dead_regs, regno);
697 if (regno >= FIRST_PSEUDO_REGISTER && reg->subreg_p)
698 bitmap_set_bit (&subreg_regs, regno);
699 }
700 if (CALL_P (insn))
701 for (int i = 0; i < call_used_regs_arr_len; i++)
702 bitmap_set_bit (&get_remat_bb_data (bb)->dead_regs,
703 call_used_regs_arr[i]);
704 }
705
706 /* Calculate changed_regs and dead_regs for each BB. */
707 static void
708 calculate_local_reg_remat_bb_data (void)
709 {
710 basic_block bb;
711 rtx_insn *insn;
712
713 FOR_EACH_BB_FN (bb, cfun)
714 FOR_BB_INSNS (bb, insn)
715 if (NONDEBUG_INSN_P (insn))
716 set_bb_regs (bb, insn);
717 }
718
719 \f
720
721 /* Return true if REGNO is an input operand of INSN. */
722 static bool
723 input_regno_present_p (rtx_insn *insn, int regno)
724 {
725 int iter;
726 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
727 struct lra_static_insn_data *static_id = id->insn_static_data;
728 struct lra_insn_reg *reg;
729
730 for (iter = 0; iter < 2; iter++)
731 for (reg = (iter == 0 ? id->regs : static_id->hard_regs);
732 reg != NULL;
733 reg = reg->next)
734 if (reg->type == OP_IN && reg->regno == regno)
735 return true;
736 return false;
737 }
738
739 /* Return true if a call used register is an input operand of INSN. */
740 static bool
741 call_used_input_regno_present_p (rtx_insn *insn)
742 {
743 int iter;
744 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
745 struct lra_static_insn_data *static_id = id->insn_static_data;
746 struct lra_insn_reg *reg;
747
748 for (iter = 0; iter < 2; iter++)
749 for (reg = (iter == 0 ? id->regs : static_id->hard_regs);
750 reg != NULL;
751 reg = reg->next)
752 if (reg->type == OP_IN && reg->regno <= FIRST_PSEUDO_REGISTER
753 && TEST_HARD_REG_BIT (call_used_reg_set, reg->regno))
754 return true;
755 return false;
756 }
757
758 /* Calculate livein_cands for each BB. */
759 static void
760 calculate_livein_cands (void)
761 {
762 basic_block bb;
763
764 FOR_EACH_BB_FN (bb, cfun)
765 {
766 bitmap livein_regs = df_get_live_in (bb);
767 bitmap livein_cands = &get_remat_bb_data (bb)->livein_cands;
768 for (unsigned int i = 0; i < cands_num; i++)
769 {
770 cand_t cand = all_cands[i];
771 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
772 struct lra_insn_reg *reg;
773
774 for (reg = id->regs; reg != NULL; reg = reg->next)
775 if (reg->type == OP_IN && ! bitmap_bit_p (livein_regs, reg->regno))
776 break;
777 if (reg == NULL)
778 bitmap_set_bit (livein_cands, i);
779 }
780 }
781 }
782
783 /* Calculate gen_cands for each BB. */
784 static void
785 calculate_gen_cands (void)
786 {
787 basic_block bb;
788 bitmap gen_cands;
789 bitmap_head gen_insns;
790 rtx_insn *insn;
791
792 bitmap_initialize (&gen_insns, &reg_obstack);
793 FOR_EACH_BB_FN (bb, cfun)
794 {
795 gen_cands = &get_remat_bb_data (bb)->gen_cands;
796 bitmap_clear (&gen_insns);
797 FOR_BB_INSNS (bb, insn)
798 if (INSN_P (insn))
799 {
800 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
801 struct lra_static_insn_data *static_id = id->insn_static_data;
802 struct lra_insn_reg *reg;
803 unsigned int uid;
804 bitmap_iterator bi;
805 cand_t cand;
806 rtx set;
807 int iter;
808 int src_regno = -1, dst_regno = -1;
809
810 if ((set = single_set (insn)) != NULL
811 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
812 {
813 src_regno = REGNO (SET_SRC (set));
814 dst_regno = REGNO (SET_DEST (set));
815 }
816
817 /* Update gen_cands: */
818 bitmap_clear (&temp_bitmap);
819 for (iter = 0; iter < 2; iter++)
820 for (reg = (iter == 0 ? id->regs : static_id->hard_regs);
821 reg != NULL;
822 reg = reg->next)
823 if (reg->type != OP_IN
824 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
825 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
826 {
827 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
828
829 cand = insn_to_cand[INSN_UID (insn2)];
830 gcc_assert (cand != NULL);
831 /* Ignore the reload insn. */
832 if (src_regno == cand->reload_regno
833 && dst_regno == cand->regno)
834 continue;
835 if (cand->regno == reg->regno
836 || input_regno_present_p (insn2, reg->regno))
837 {
838 bitmap_clear_bit (gen_cands, cand->index);
839 bitmap_set_bit (&temp_bitmap, uid);
840 }
841 }
842
843 if (CALL_P (insn))
844 EXECUTE_IF_SET_IN_BITMAP (&gen_insns, 0, uid, bi)
845 {
846 rtx_insn *insn2 = lra_insn_recog_data[uid]->insn;
847
848 cand = insn_to_cand[INSN_UID (insn2)];
849 gcc_assert (cand != NULL);
850 if (call_used_input_regno_present_p (insn2))
851 {
852 bitmap_clear_bit (gen_cands, cand->index);
853 bitmap_set_bit (&temp_bitmap, uid);
854 }
855 }
856 bitmap_and_compl_into (&gen_insns, &temp_bitmap);
857
858 cand = insn_to_cand[INSN_UID (insn)];
859 if (cand != NULL)
860 {
861 bitmap_set_bit (gen_cands, cand->index);
862 bitmap_set_bit (&gen_insns, INSN_UID (insn));
863 }
864 }
865 }
866 bitmap_clear (&gen_insns);
867 }
868
869 \f
870
871 /* The common transfer function used by the DF equation solver to
872 propagate (partial) availability info BB_IN to BB_OUT through block
873 with BB_INDEX according to the following equation:
874
875 bb.out = ((bb.in & bb.livein) - bb.killed) OR bb.gen
876 */
877 static bool
878 cand_trans_fun (int bb_index, bitmap bb_in, bitmap bb_out)
879 {
880 remat_bb_data_t bb_info;
881 bitmap bb_livein, bb_changed_regs, bb_dead_regs;
882 unsigned int cid;
883 bitmap_iterator bi;
884
885 bb_info = get_remat_bb_data_by_index (bb_index);
886 bb_livein = &bb_info->livein_cands;
887 bb_changed_regs = &bb_info->changed_regs;
888 bb_dead_regs = &bb_info->dead_regs;
889 /* Calculate killed avin cands -- cands whose regs are changed or
890 becoming dead in the BB. We calculate it here as we hope that
891 repeated calculations are compensated by smaller size of BB_IN in
892 comparison with all candidates number. */
893 bitmap_clear (&temp_bitmap);
894 EXECUTE_IF_SET_IN_BITMAP (bb_in, 0, cid, bi)
895 {
896 cand_t cand = all_cands[cid];
897 lra_insn_recog_data_t id = lra_get_insn_recog_data (cand->insn);
898 struct lra_insn_reg *reg;
899
900 if (! bitmap_bit_p (bb_livein, cid))
901 {
902 bitmap_set_bit (&temp_bitmap, cid);
903 continue;
904 }
905 for (reg = id->regs; reg != NULL; reg = reg->next)
906 /* Ignore all outputs which are not the regno for
907 rematerialization. */
908 if (reg->type == OP_OUT && reg->regno != cand->regno)
909 continue;
910 else if (bitmap_bit_p (bb_changed_regs, reg->regno)
911 || bitmap_bit_p (bb_dead_regs, reg->regno))
912 {
913 bitmap_set_bit (&temp_bitmap, cid);
914 break;
915 }
916 /* Check regno for rematerialization. */
917 if (bitmap_bit_p (bb_changed_regs, cand->regno)
918 || bitmap_bit_p (bb_dead_regs, cand->regno))
919 bitmap_set_bit (&temp_bitmap, cid);
920 }
921 return bitmap_ior_and_compl (bb_out,
922 &bb_info->gen_cands, bb_in, &temp_bitmap);
923 }
924
925 \f
926
927 /* The transfer function used by the DF equation solver to propagate
928 partial candidate availability info through block with BB_INDEX
929 according to the following equation:
930
931 bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen
932 */
933 static bool
934 cand_pav_trans_fun (int bb_index)
935 {
936 remat_bb_data_t bb_info;
937
938 bb_info = get_remat_bb_data_by_index (bb_index);
939 return cand_trans_fun (bb_index, &bb_info->pavin_cands,
940 &bb_info->pavout_cands);
941 }
942
943 /* The confluence function used by the DF equation solver to set up
944 cand_pav info for a block BB without predecessor. */
945 static void
946 cand_pav_con_fun_0 (basic_block bb)
947 {
948 bitmap_clear (&get_remat_bb_data (bb)->pavin_cands);
949 }
950
951 /* The confluence function used by the DF equation solver to propagate
952 partial candidate availability info from predecessor to successor
953 on edge E (pred->bb) according to the following equation:
954
955 bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)
956 */
957 static bool
958 cand_pav_con_fun_n (edge e)
959 {
960 basic_block pred = e->src;
961 basic_block bb = e->dest;
962 remat_bb_data_t bb_info;
963 bitmap bb_pavin, pred_pavout;
964
965 bb_info = get_remat_bb_data (bb);
966 bb_pavin = &bb_info->pavin_cands;
967 pred_pavout = &get_remat_bb_data (pred)->pavout_cands;
968 return bitmap_ior_into (bb_pavin, pred_pavout);
969 }
970
971 \f
972
973 /* The transfer function used by the DF equation solver to propagate
974 candidate availability info through block with BB_INDEX according
975 to the following equation:
976
977 bb.avout = ((bb.avin & bb.livein) - bb.killed) OR bb.gen
978 */
979 static bool
980 cand_av_trans_fun (int bb_index)
981 {
982 remat_bb_data_t bb_info;
983
984 bb_info = get_remat_bb_data_by_index (bb_index);
985 return cand_trans_fun (bb_index, &bb_info->avin_cands,
986 &bb_info->avout_cands);
987 }
988
989 /* The confluence function used by the DF equation solver to set up
990 cand_av info for a block BB without predecessor. */
991 static void
992 cand_av_con_fun_0 (basic_block bb)
993 {
994 bitmap_clear (&get_remat_bb_data (bb)->avin_cands);
995 }
996
997 /* The confluence function used by the DF equation solver to propagate
998 cand_av info from predecessor to successor on edge E (pred->bb)
999 according to the following equation:
1000
1001 bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)
1002 */
1003 static bool
1004 cand_av_con_fun_n (edge e)
1005 {
1006 basic_block pred = e->src;
1007 basic_block bb = e->dest;
1008 remat_bb_data_t bb_info;
1009 bitmap bb_avin, pred_avout;
1010
1011 bb_info = get_remat_bb_data (bb);
1012 bb_avin = &bb_info->avin_cands;
1013 pred_avout = &get_remat_bb_data (pred)->avout_cands;
1014 return bitmap_and_into (bb_avin, pred_avout);
1015 }
1016
1017 /* Calculate available candidates for each BB. */
1018 static void
1019 calculate_global_remat_bb_data (void)
1020 {
1021 basic_block bb;
1022
1023 df_simple_dataflow
1024 (DF_FORWARD, NULL, cand_pav_con_fun_0, cand_pav_con_fun_n,
1025 cand_pav_trans_fun, &all_blocks,
1026 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
1027 /* Initialize avin by pavin. */
1028 FOR_EACH_BB_FN (bb, cfun)
1029 bitmap_copy (&get_remat_bb_data (bb)->avin_cands,
1030 &get_remat_bb_data (bb)->pavin_cands);
1031 df_simple_dataflow
1032 (DF_FORWARD, NULL, cand_av_con_fun_0, cand_av_con_fun_n,
1033 cand_av_trans_fun, &all_blocks,
1034 df_get_postorder (DF_FORWARD), df_get_n_blocks (DF_FORWARD));
1035 }
1036
1037 \f
1038
1039 /* Setup sp offset attribute to SP_OFFSET for all INSNS. */
1040 static void
1041 change_sp_offset (rtx_insn *insns, HOST_WIDE_INT sp_offset)
1042 {
1043 for (rtx_insn *insn = insns; insn != NULL; insn = NEXT_INSN (insn))
1044 eliminate_regs_in_insn (insn, false, false, sp_offset);
1045 }
1046
1047 /* Return start hard register of REG (can be a hard or a pseudo reg)
1048 or -1 (if it is a spilled pseudo). Return number of hard registers
1049 occupied by REG through parameter NREGS if the start hard reg is
1050 not negative. */
1051 static int
1052 get_hard_regs (struct lra_insn_reg *reg, int &nregs)
1053 {
1054 int regno = reg->regno;
1055 int hard_regno = regno < FIRST_PSEUDO_REGISTER ? regno : reg_renumber[regno];
1056
1057 if (hard_regno >= 0)
1058 nregs = hard_regno_nregs[hard_regno][reg->biggest_mode];
1059 return hard_regno;
1060 }
1061
1062 /* Make copy of and register scratch pseudos in rematerialized insn
1063 REMAT_INSN. */
1064 static void
1065 update_scratch_ops (rtx_insn *remat_insn)
1066 {
1067 lra_insn_recog_data_t id = lra_get_insn_recog_data (remat_insn);
1068 struct lra_static_insn_data *static_id = id->insn_static_data;
1069 for (int i = 0; i < static_id->n_operands; i++)
1070 {
1071 rtx *loc = id->operand_loc[i];
1072 if (! REG_P (*loc))
1073 continue;
1074 int regno = REGNO (*loc);
1075 if (! lra_former_scratch_p (regno))
1076 continue;
1077 *loc = lra_create_new_reg (GET_MODE (*loc), *loc,
1078 lra_get_allocno_class (regno),
1079 "scratch pseudo copy");
1080 lra_register_new_scratch_op (remat_insn, i);
1081 }
1082
1083 }
1084
1085 /* Insert rematerialization insns using the data-flow data calculated
1086 earlier. */
1087 static bool
1088 do_remat (void)
1089 {
1090 rtx_insn *insn;
1091 basic_block bb;
1092 bitmap_head avail_cands;
1093 bitmap_head active_cands;
1094 bool changed_p = false;
1095 /* Living hard regs and hard registers of living pseudos. */
1096 HARD_REG_SET live_hard_regs;
1097
1098 bitmap_initialize (&avail_cands, &reg_obstack);
1099 bitmap_initialize (&active_cands, &reg_obstack);
1100 FOR_EACH_BB_FN (bb, cfun)
1101 {
1102 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_out (bb));
1103 bitmap_and (&avail_cands, &get_remat_bb_data (bb)->avin_cands,
1104 &get_remat_bb_data (bb)->livein_cands);
1105 /* Activating insns are always in the same block as their corresponding
1106 remat insn, so at the start of a block the two bitsets are equal. */
1107 bitmap_copy (&active_cands, &avail_cands);
1108 FOR_BB_INSNS (bb, insn)
1109 {
1110 if (!NONDEBUG_INSN_P (insn))
1111 continue;
1112
1113 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1114 struct lra_static_insn_data *static_id = id->insn_static_data;
1115 struct lra_insn_reg *reg;
1116 cand_t cand;
1117 unsigned int cid;
1118 bitmap_iterator bi;
1119 rtx set;
1120 int iter;
1121 int src_regno = -1, dst_regno = -1;
1122
1123 if ((set = single_set (insn)) != NULL
1124 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set)))
1125 {
1126 src_regno = REGNO (SET_SRC (set));
1127 dst_regno = REGNO (SET_DEST (set));
1128 }
1129
1130 cand = NULL;
1131 /* Check possibility of rematerialization (hard reg or
1132 unpsilled pseudo <- spilled pseudo): */
1133 if (dst_regno >= 0 && src_regno >= FIRST_PSEUDO_REGISTER
1134 && reg_renumber[src_regno] < 0
1135 && (dst_regno < FIRST_PSEUDO_REGISTER
1136 || reg_renumber[dst_regno] >= 0))
1137 {
1138 for (cand = regno_cands[src_regno];
1139 cand != NULL;
1140 cand = cand->next_regno_cand)
1141 if (bitmap_bit_p (&avail_cands, cand->index)
1142 && bitmap_bit_p (&active_cands, cand->index))
1143 break;
1144 }
1145 int i, hard_regno, nregs;
1146 rtx_insn *remat_insn = NULL;
1147 HOST_WIDE_INT cand_sp_offset = 0;
1148 if (cand != NULL)
1149 {
1150 lra_insn_recog_data_t cand_id
1151 = lra_get_insn_recog_data (cand->insn);
1152 struct lra_static_insn_data *static_cand_id
1153 = cand_id->insn_static_data;
1154 rtx saved_op = *cand_id->operand_loc[cand->nop];
1155
1156 /* Check clobbers do not kill something living. */
1157 gcc_assert (REG_P (saved_op));
1158 int ignore_regno = REGNO (saved_op);
1159
1160 for (reg = cand_id->regs; reg != NULL; reg = reg->next)
1161 if (reg->type != OP_IN && reg->regno != ignore_regno)
1162 {
1163 hard_regno = get_hard_regs (reg, nregs);
1164 gcc_assert (hard_regno >= 0);
1165 for (i = 0; i < nregs; i++)
1166 if (TEST_HARD_REG_BIT (live_hard_regs, hard_regno + i))
1167 break;
1168 if (i < nregs)
1169 break;
1170 }
1171
1172 if (reg == NULL)
1173 {
1174 for (reg = static_cand_id->hard_regs;
1175 reg != NULL;
1176 reg = reg->next)
1177 if (reg->type != OP_IN
1178 && TEST_HARD_REG_BIT (live_hard_regs, reg->regno))
1179 break;
1180 }
1181
1182 if (reg == NULL)
1183 {
1184 *cand_id->operand_loc[cand->nop] = SET_DEST (set);
1185 lra_update_insn_regno_info (cand->insn);
1186 bool ok_p = lra_constrain_insn (cand->insn);
1187 if (ok_p)
1188 {
1189 rtx remat_pat = copy_insn (PATTERN (cand->insn));
1190
1191 start_sequence ();
1192 emit_insn (remat_pat);
1193 remat_insn = get_insns ();
1194 end_sequence ();
1195 if (recog_memoized (remat_insn) < 0)
1196 remat_insn = NULL;
1197 cand_sp_offset = cand_id->sp_offset;
1198 }
1199 *cand_id->operand_loc[cand->nop] = saved_op;
1200 lra_update_insn_regno_info (cand->insn);
1201 }
1202 }
1203
1204 bitmap_clear (&temp_bitmap);
1205 /* Update avail_cands (see analogous code for
1206 calculate_gen_cands). */
1207 for (iter = 0; iter < 2; iter++)
1208 for (reg = (iter == 0 ? id->regs : static_id->hard_regs);
1209 reg != NULL;
1210 reg = reg->next)
1211 if (reg->type != OP_IN
1212 || find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1213 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1214 {
1215 cand = all_cands[cid];
1216
1217 /* Ignore the reload insn. */
1218 if (src_regno == cand->reload_regno
1219 && dst_regno == cand->regno)
1220 continue;
1221 if (cand->regno == reg->regno
1222 || input_regno_present_p (cand->insn, reg->regno))
1223 bitmap_set_bit (&temp_bitmap, cand->index);
1224 }
1225
1226 if (CALL_P (insn))
1227 EXECUTE_IF_SET_IN_BITMAP (&avail_cands, 0, cid, bi)
1228 {
1229 cand = all_cands[cid];
1230
1231 if (call_used_input_regno_present_p (cand->insn))
1232 bitmap_set_bit (&temp_bitmap, cand->index);
1233 }
1234
1235 bitmap_and_compl_into (&avail_cands, &temp_bitmap);
1236
1237 /* Now see whether a candidate is made active or available
1238 by this insn. */
1239 cand = insn_to_cand_activation[INSN_UID (insn)];
1240 if (cand)
1241 bitmap_set_bit (&active_cands, cand->index);
1242
1243 cand = insn_to_cand[INSN_UID (insn)];
1244 if (cand != NULL)
1245 {
1246 bitmap_set_bit (&avail_cands, cand->index);
1247 if (cand->reload_regno == -1)
1248 bitmap_set_bit (&active_cands, cand->index);
1249 else
1250 bitmap_clear_bit (&active_cands, cand->index);
1251 }
1252
1253 if (remat_insn != NULL)
1254 {
1255 HOST_WIDE_INT sp_offset_change = cand_sp_offset - id->sp_offset;
1256 if (sp_offset_change != 0)
1257 change_sp_offset (remat_insn, sp_offset_change);
1258 update_scratch_ops (remat_insn);
1259 lra_process_new_insns (insn, remat_insn, NULL,
1260 "Inserting rematerialization insn");
1261 lra_set_insn_deleted (insn);
1262 changed_p = true;
1263 continue;
1264 }
1265
1266 /* Update live hard regs: */
1267 for (reg = id->regs; reg != NULL; reg = reg->next)
1268 if (reg->type == OP_IN
1269 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1270 {
1271 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1272 continue;
1273 for (i = 0; i < nregs; i++)
1274 CLEAR_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1275 }
1276 /* Process also hard regs (e.g. CC register) which are part
1277 of insn definition. */
1278 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1279 if (reg->type == OP_IN
1280 && find_regno_note (insn, REG_DEAD, reg->regno) != NULL)
1281 CLEAR_HARD_REG_BIT (live_hard_regs, reg->regno);
1282 /* Inputs have been processed, now process outputs. */
1283 for (reg = id->regs; reg != NULL; reg = reg->next)
1284 if (reg->type != OP_IN
1285 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1286 {
1287 if ((hard_regno = get_hard_regs (reg, nregs)) < 0)
1288 continue;
1289 for (i = 0; i < nregs; i++)
1290 SET_HARD_REG_BIT (live_hard_regs, hard_regno + i);
1291 }
1292 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
1293 if (reg->type != OP_IN
1294 && find_regno_note (insn, REG_UNUSED, reg->regno) == NULL)
1295 SET_HARD_REG_BIT (live_hard_regs, reg->regno);
1296 }
1297 }
1298 bitmap_clear (&avail_cands);
1299 bitmap_clear (&active_cands);
1300 return changed_p;
1301 }
1302
1303 \f
1304
1305 /* Current number of rematerialization iteration. */
1306 int lra_rematerialization_iter;
1307
1308 /* Entry point of the rematerialization sub-pass. Return true if we
1309 did any rematerialization. */
1310 bool
1311 lra_remat (void)
1312 {
1313 basic_block bb;
1314 bool result;
1315 int max_regno = max_reg_num ();
1316
1317 if (! flag_lra_remat)
1318 return false;
1319 lra_rematerialization_iter++;
1320 if (lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
1321 return false;
1322 if (lra_dump_file != NULL)
1323 fprintf (lra_dump_file,
1324 "\n******** Rematerialization #%d: ********\n\n",
1325 lra_rematerialization_iter);
1326 timevar_push (TV_LRA_REMAT);
1327 insn_to_cand = XCNEWVEC (cand_t, get_max_uid ());
1328 insn_to_cand_activation = XCNEWVEC (cand_t, get_max_uid ());
1329 regno_cands = XCNEWVEC (cand_t, max_regno);
1330 all_cands.create (8000);
1331 call_used_regs_arr_len = 0;
1332 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1333 if (call_used_regs[i])
1334 call_used_regs_arr[call_used_regs_arr_len++] = i;
1335 initiate_cand_table ();
1336 create_remat_bb_data ();
1337 bitmap_initialize (&temp_bitmap, &reg_obstack);
1338 bitmap_initialize (&subreg_regs, &reg_obstack);
1339 calculate_local_reg_remat_bb_data ();
1340 create_cands ();
1341 calculate_livein_cands ();
1342 calculate_gen_cands ();
1343 bitmap_initialize (&all_blocks, &reg_obstack);
1344 FOR_ALL_BB_FN (bb, cfun)
1345 bitmap_set_bit (&all_blocks, bb->index);
1346 calculate_global_remat_bb_data ();
1347 dump_candidates_and_remat_bb_data ();
1348 result = do_remat ();
1349 all_cands.release ();
1350 bitmap_clear (&temp_bitmap);
1351 bitmap_clear (&subreg_regs);
1352 finish_remat_bb_data ();
1353 finish_cand_table ();
1354 bitmap_clear (&all_blocks);
1355 free (regno_cands);
1356 free (insn_to_cand);
1357 free (insn_to_cand_activation);
1358 timevar_pop (TV_LRA_REMAT);
1359 return result;
1360 }