lra.cc revision 1.1.1.1 1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2022 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov (at) 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
22 /* The Local Register Allocator (LRA) is a replacement of former
23 reload pass. It is focused to simplify code solving the reload
24 pass tasks, to make the code maintenance easier, and to implement new
25 perspective optimizations.
26
27 The major LRA design solutions are:
28 o division small manageable, separated sub-tasks
29 o reflection of all transformations and decisions in RTL as more
30 as possible
31 o insn constraints as a primary source of the info (minimizing
32 number of target-depended macros/hooks)
33
34 In brief LRA works by iterative insn process with the final goal is
35 to satisfy all insn and address constraints:
36 o New reload insns (in brief reloads) and reload pseudos might be
37 generated;
38 o Some pseudos might be spilled to assign hard registers to
39 new reload pseudos;
40 o Recalculating spilled pseudo values (rematerialization);
41 o Changing spilled pseudos to stack memory or their equivalences;
42 o Allocation stack memory changes the address displacement and
43 new iteration is needed.
44
45 Here is block diagram of LRA passes:
46
47 ------------------------
48 --------------- | Undo inheritance for | ---------------
49 | Memory-memory | | spilled pseudos, | | New (and old) |
50 | move coalesce |<---| splits for pseudos got |<-- | pseudos |
51 --------------- | the same hard regs, | | assignment |
52 Start | | and optional reloads | ---------------
53 | | ------------------------ ^
54 V | ---------------- |
55 ----------- V | Update virtual | |
56 | Remove |----> ------------>| register | |
57 | scratches | ^ | displacements | |
58 ----------- | ---------------- |
59 | | |
60 | V New |
61 | ------------ pseudos -------------------
62 | |Constraints:| or insns | Inheritance/split |
63 | | RTL |--------->| transformations |
64 | | transfor- | | in EBB scope |
65 | substi- | mations | -------------------
66 | tutions ------------
67 | | No change
68 ---------------- V
69 | Spilled pseudo | -------------------
70 | to memory |<----| Rematerialization |
71 | substitution | -------------------
72 ----------------
73 | No susbtitions
74 V
75 -------------------------
76 | Hard regs substitution, |
77 | devirtalization, and |------> Finish
78 | restoring scratches got |
79 | memory |
80 -------------------------
81
82 To speed up the process:
83 o We process only insns affected by changes on previous
84 iterations;
85 o We don't use DFA-infrastructure because it results in much slower
86 compiler speed than a special IR described below does;
87 o We use a special insn representation for quick access to insn
88 info which is always *synchronized* with the current RTL;
89 o Insn IR is minimized by memory. It is divided on three parts:
90 o one specific for each insn in RTL (only operand locations);
91 o one common for all insns in RTL with the same insn code
92 (different operand attributes from machine descriptions);
93 o one oriented for maintenance of live info (list of pseudos).
94 o Pseudo data:
95 o all insns where the pseudo is referenced;
96 o live info (conflicting hard regs, live ranges, # of
97 references etc);
98 o data used for assigning (preferred hard regs, costs etc).
99
100 This file contains LRA driver, LRA utility functions and data, and
101 code for dealing with scratches. */
102
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "backend.h"
107 #include "target.h"
108 #include "rtl.h"
109 #include "tree.h"
110 #include "predict.h"
111 #include "df.h"
112 #include "memmodel.h"
113 #include "tm_p.h"
114 #include "optabs.h"
115 #include "regs.h"
116 #include "ira.h"
117 #include "recog.h"
118 #include "expr.h"
119 #include "cfgrtl.h"
120 #include "cfgbuild.h"
121 #include "lra.h"
122 #include "lra-int.h"
123 #include "print-rtl.h"
124 #include "function-abi.h"
125
126 /* Dump bitmap SET with TITLE and BB INDEX. */
127 void
128 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
129 {
130 unsigned int i;
131 int count;
132 bitmap_iterator bi;
133 static const int max_nums_on_line = 10;
134
135 if (bitmap_empty_p (set))
136 return;
137 fprintf (lra_dump_file, " %s %d:", title, index);
138 fprintf (lra_dump_file, "\n");
139 count = max_nums_on_line + 1;
140 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
141 {
142 if (count > max_nums_on_line)
143 {
144 fprintf (lra_dump_file, "\n ");
145 count = 0;
146 }
147 fprintf (lra_dump_file, " %4u", i);
148 count++;
149 }
150 fprintf (lra_dump_file, "\n");
151 }
152
153 /* Hard registers currently not available for allocation. It can
154 changed after some hard registers become not eliminable. */
155 HARD_REG_SET lra_no_alloc_regs;
156
157 static int get_new_reg_value (void);
158 static void expand_reg_info (void);
159 static void invalidate_insn_recog_data (int);
160 static int get_insn_freq (rtx_insn *);
161 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
162 rtx_insn *, int);
163 /* Expand all regno related info needed for LRA. */
164 static void
165 expand_reg_data (int old)
166 {
167 resize_reg_info ();
168 expand_reg_info ();
169 ira_expand_reg_equiv ();
170 for (int i = (int) max_reg_num () - 1; i >= old; i--)
171 lra_change_class (i, ALL_REGS, " Set", true);
172 }
173
174 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
175 or of VOIDmode, use MD_MODE for the new reg. Initialize its
176 register class to RCLASS. Print message about assigning class
177 RCLASS containing new register name TITLE unless it is NULL. Use
178 attributes of ORIGINAL if it is a register. The created register
179 will have unique held value. */
180 rtx
181 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
182 enum reg_class rclass,
183 HARD_REG_SET *exclude_start_hard_regs,
184 const char *title)
185 {
186 machine_mode mode;
187 rtx new_reg;
188
189 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
190 mode = md_mode;
191 lra_assert (mode != VOIDmode);
192 new_reg = gen_reg_rtx (mode);
193 if (original == NULL_RTX || ! REG_P (original))
194 {
195 if (lra_dump_file != NULL)
196 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
197 }
198 else
199 {
200 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
201 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
202 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
203 REG_POINTER (new_reg) = REG_POINTER (original);
204 REG_ATTRS (new_reg) = REG_ATTRS (original);
205 if (lra_dump_file != NULL)
206 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
207 REGNO (new_reg), REGNO (original));
208 }
209 if (lra_dump_file != NULL)
210 {
211 if (title != NULL)
212 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
213 reg_class_names[rclass], *title == '\0' ? "" : " ",
214 title, REGNO (new_reg));
215 fprintf (lra_dump_file, "\n");
216 }
217 expand_reg_data (max_reg_num ());
218 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
219 if (exclude_start_hard_regs != NULL)
220 lra_reg_info[REGNO (new_reg)].exclude_start_hard_regs
221 = *exclude_start_hard_regs;
222 return new_reg;
223 }
224
225 /* Analogous to the previous function but also inherits value of
226 ORIGINAL. */
227 rtx
228 lra_create_new_reg (machine_mode md_mode, rtx original, enum reg_class rclass,
229 HARD_REG_SET *exclude_start_hard_regs, const char *title)
230 {
231 rtx new_reg;
232
233 new_reg
234 = lra_create_new_reg_with_unique_value (md_mode, original, rclass,
235 exclude_start_hard_regs, title);
236 if (original != NULL_RTX && REG_P (original))
237 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
238 return new_reg;
239 }
240
241 /* Set up for REGNO unique hold value. */
242 void
243 lra_set_regno_unique_value (int regno)
244 {
245 lra_reg_info[regno].val = get_new_reg_value ();
246 }
247
248 /* Invalidate INSN related info used by LRA. The info should never be
249 used after that. */
250 void
251 lra_invalidate_insn_data (rtx_insn *insn)
252 {
253 lra_invalidate_insn_regno_info (insn);
254 invalidate_insn_recog_data (INSN_UID (insn));
255 }
256
257 /* Mark INSN deleted and invalidate the insn related info used by
258 LRA. */
259 void
260 lra_set_insn_deleted (rtx_insn *insn)
261 {
262 lra_invalidate_insn_data (insn);
263 SET_INSN_DELETED (insn);
264 }
265
266 /* Delete an unneeded INSN and any previous insns who sole purpose is
267 loading data that is dead in INSN. */
268 void
269 lra_delete_dead_insn (rtx_insn *insn)
270 {
271 rtx_insn *prev = prev_real_insn (insn);
272 rtx prev_dest;
273
274 /* If the previous insn sets a register that dies in our insn,
275 delete it too. */
276 if (prev && GET_CODE (PATTERN (prev)) == SET
277 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
278 && reg_mentioned_p (prev_dest, PATTERN (insn))
279 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
280 && ! side_effects_p (SET_SRC (PATTERN (prev))))
281 lra_delete_dead_insn (prev);
282
283 lra_set_insn_deleted (insn);
284 }
285
286 /* Emit insn x = y + z. Return NULL if we failed to do it.
287 Otherwise, return the insn. We don't use gen_add3_insn as it might
288 clobber CC. */
289 static rtx_insn *
290 emit_add3_insn (rtx x, rtx y, rtx z)
291 {
292 rtx_insn *last;
293
294 last = get_last_insn ();
295
296 if (have_addptr3_insn (x, y, z))
297 {
298 rtx_insn *insn = gen_addptr3_insn (x, y, z);
299
300 /* If the target provides an "addptr" pattern it hopefully does
301 for a reason. So falling back to the normal add would be
302 a bug. */
303 lra_assert (insn != NULL_RTX);
304 emit_insn (insn);
305 return insn;
306 }
307
308 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
309 y, z)));
310 if (recog_memoized (insn) < 0)
311 {
312 delete_insns_since (last);
313 insn = NULL;
314 }
315 return insn;
316 }
317
318 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
319 last resort. */
320 static rtx_insn *
321 emit_add2_insn (rtx x, rtx y)
322 {
323 rtx_insn *insn = emit_add3_insn (x, x, y);
324 if (insn == NULL_RTX)
325 {
326 insn = gen_add2_insn (x, y);
327 if (insn != NULL_RTX)
328 emit_insn (insn);
329 }
330 return insn;
331 }
332
333 /* Target checks operands through operand predicates to recognize an
334 insn. We should have a special precaution to generate add insns
335 which are frequent results of elimination.
336
337 Emit insns for x = y + z. X can be used to store intermediate
338 values and should be not in Y and Z when we use X to store an
339 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
340 + disp] where base and index are registers, disp and scale are
341 constants. Y should contain base if it is present, Z should
342 contain disp if any. index[*scale] can be part of Y or Z. */
343 void
344 lra_emit_add (rtx x, rtx y, rtx z)
345 {
346 int old;
347 rtx_insn *last;
348 rtx a1, a2, base, index, disp, scale, index_scale;
349 bool ok_p;
350
351 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
352 old = max_reg_num ();
353 if (add3_insn != NULL)
354 ;
355 else
356 {
357 disp = a2 = NULL_RTX;
358 if (GET_CODE (y) == PLUS)
359 {
360 a1 = XEXP (y, 0);
361 a2 = XEXP (y, 1);
362 disp = z;
363 }
364 else
365 {
366 a1 = y;
367 if (CONSTANT_P (z))
368 disp = z;
369 else
370 a2 = z;
371 }
372 index_scale = scale = NULL_RTX;
373 if (GET_CODE (a1) == MULT)
374 {
375 index_scale = a1;
376 index = XEXP (a1, 0);
377 scale = XEXP (a1, 1);
378 base = a2;
379 }
380 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
381 {
382 index_scale = a2;
383 index = XEXP (a2, 0);
384 scale = XEXP (a2, 1);
385 base = a1;
386 }
387 else
388 {
389 base = a1;
390 index = a2;
391 }
392 if ((base != NULL_RTX && ! (REG_P (base) || GET_CODE (base) == SUBREG))
393 || (index != NULL_RTX
394 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
395 || (disp != NULL_RTX && ! CONSTANT_P (disp))
396 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
397 {
398 /* Probably we have no 3 op add. Last chance is to use 2-op
399 add insn. To succeed, don't move Z to X as an address
400 segment always comes in Y. Otherwise, we might fail when
401 adding the address segment to register. */
402 lra_assert (x != y && x != z);
403 emit_move_insn (x, y);
404 rtx_insn *insn = emit_add2_insn (x, z);
405 lra_assert (insn != NULL_RTX);
406 }
407 else
408 {
409 if (index_scale == NULL_RTX)
410 index_scale = index;
411 if (disp == NULL_RTX)
412 {
413 /* Generate x = index_scale; x = x + base. */
414 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
415 emit_move_insn (x, index_scale);
416 rtx_insn *insn = emit_add2_insn (x, base);
417 lra_assert (insn != NULL_RTX);
418 }
419 else if (scale == NULL_RTX)
420 {
421 /* Try x = base + disp. */
422 lra_assert (base != NULL_RTX);
423 last = get_last_insn ();
424 rtx_insn *move_insn =
425 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
426 if (recog_memoized (move_insn) < 0)
427 {
428 delete_insns_since (last);
429 /* Generate x = disp; x = x + base. */
430 emit_move_insn (x, disp);
431 rtx_insn *add2_insn = emit_add2_insn (x, base);
432 lra_assert (add2_insn != NULL_RTX);
433 }
434 /* Generate x = x + index. */
435 if (index != NULL_RTX)
436 {
437 rtx_insn *insn = emit_add2_insn (x, index);
438 lra_assert (insn != NULL_RTX);
439 }
440 }
441 else
442 {
443 /* Try x = index_scale; x = x + disp; x = x + base. */
444 last = get_last_insn ();
445 rtx_insn *move_insn = emit_move_insn (x, index_scale);
446 ok_p = false;
447 if (recog_memoized (move_insn) >= 0)
448 {
449 rtx_insn *insn = emit_add2_insn (x, disp);
450 if (insn != NULL_RTX)
451 {
452 if (base == NULL_RTX)
453 ok_p = true;
454 else
455 {
456 insn = emit_add2_insn (x, base);
457 if (insn != NULL_RTX)
458 ok_p = true;
459 }
460 }
461 }
462 if (! ok_p)
463 {
464 rtx_insn *insn;
465
466 delete_insns_since (last);
467 /* Generate x = disp; x = x + base; x = x + index_scale. */
468 emit_move_insn (x, disp);
469 if (base != NULL_RTX)
470 {
471 insn = emit_add2_insn (x, base);
472 lra_assert (insn != NULL_RTX);
473 }
474 insn = emit_add2_insn (x, index_scale);
475 lra_assert (insn != NULL_RTX);
476 }
477 }
478 }
479 }
480 /* Functions emit_... can create pseudos -- so expand the pseudo
481 data. */
482 if (old != max_reg_num ())
483 expand_reg_data (old);
484 }
485
486 /* The number of emitted reload insns so far. */
487 int lra_curr_reload_num;
488
489 static void remove_insn_scratches (rtx_insn *insn);
490
491 /* Emit x := y, processing special case when y = u + v or y = u + v *
492 scale + w through emit_add (Y can be an address which is base +
493 index reg * scale + displacement in general case). X may be used
494 as intermediate result therefore it should be not in Y. */
495 void
496 lra_emit_move (rtx x, rtx y)
497 {
498 int old;
499 rtx_insn *insn;
500
501 if (GET_CODE (y) != PLUS)
502 {
503 if (rtx_equal_p (x, y))
504 return;
505 old = max_reg_num ();
506
507 insn = (GET_CODE (x) != STRICT_LOW_PART
508 ? emit_move_insn (x, y) : emit_insn (gen_rtx_SET (x, y)));
509 /* The move pattern may require scratch registers, so convert them
510 into real registers now. */
511 if (insn != NULL_RTX)
512 remove_insn_scratches (insn);
513 if (REG_P (x))
514 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
515 /* Function emit_move can create pseudos -- so expand the pseudo
516 data. */
517 if (old != max_reg_num ())
518 expand_reg_data (old);
519 return;
520 }
521 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
522 }
523
524 /* Update insn operands which are duplication of operands whose
525 numbers are in array of NOPS (with end marker -1). The insn is
526 represented by its LRA internal representation ID. */
527 void
528 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
529 {
530 int i, j, nop;
531 struct lra_static_insn_data *static_id = id->insn_static_data;
532
533 for (i = 0; i < static_id->n_dups; i++)
534 for (j = 0; (nop = nops[j]) >= 0; j++)
535 if (static_id->dup_num[i] == nop)
536 *id->dup_loc[i] = *id->operand_loc[nop];
537 }
538
539
540
542 /* This page contains code dealing with info about registers in the
543 insns. */
544
545 /* Pools for insn reg info. */
546 object_allocator<lra_insn_reg> lra_insn_reg_pool ("insn regs");
547
548 /* Create LRA insn related info about a reference to REGNO in INSN
549 with TYPE (in/out/inout), biggest reference mode MODE, flag that it
550 is reference through subreg (SUBREG_P), and reference to the next
551 insn reg info (NEXT). If REGNO can be early clobbered,
552 alternatives in which it can be early clobbered are given by
553 EARLY_CLOBBER_ALTS. */
554 static struct lra_insn_reg *
555 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
556 machine_mode mode, bool subreg_p,
557 alternative_mask early_clobber_alts,
558 struct lra_insn_reg *next)
559 {
560 lra_insn_reg *ir = lra_insn_reg_pool.allocate ();
561 ir->type = type;
562 ir->biggest_mode = mode;
563 if (NONDEBUG_INSN_P (insn)
564 && partial_subreg_p (lra_reg_info[regno].biggest_mode, mode))
565 lra_reg_info[regno].biggest_mode = mode;
566 ir->subreg_p = subreg_p;
567 ir->early_clobber_alts = early_clobber_alts;
568 ir->regno = regno;
569 ir->next = next;
570 return ir;
571 }
572
573 /* Free insn reg info list IR. */
574 static void
575 free_insn_regs (struct lra_insn_reg *ir)
576 {
577 struct lra_insn_reg *next_ir;
578
579 for (; ir != NULL; ir = next_ir)
580 {
581 next_ir = ir->next;
582 lra_insn_reg_pool.remove (ir);
583 }
584 }
585
586 /* Finish pool for insn reg info. */
587 static void
588 finish_insn_regs (void)
589 {
590 lra_insn_reg_pool.release ();
591 }
592
593
594
596 /* This page contains code dealing LRA insn info (or in other words
597 LRA internal insn representation). */
598
599 /* Map INSN_CODE -> the static insn data. This info is valid during
600 all translation unit. */
601 struct lra_static_insn_data *insn_code_data[NUM_INSN_CODES];
602
603 /* Debug insns are represented as a special insn with one input
604 operand which is RTL expression in var_location. */
605
606 /* The following data are used as static insn operand data for all
607 debug insns. If structure lra_operand_data is changed, the
608 initializer should be changed too. */
609 static struct lra_operand_data debug_operand_data =
610 {
611 NULL, /* alternative */
612 0, /* early_clobber_alts */
613 E_VOIDmode, /* We are not interesting in the operand mode. */
614 OP_IN,
615 0, 0, 0
616 };
617
618 /* The following data are used as static insn data for all debug
619 bind insns. If structure lra_static_insn_data is changed, the
620 initializer should be changed too. */
621 static struct lra_static_insn_data debug_bind_static_data =
622 {
623 &debug_operand_data,
624 0, /* Duplication operands #. */
625 -1, /* Commutative operand #. */
626 1, /* Operands #. There is only one operand which is debug RTL
627 expression. */
628 0, /* Duplications #. */
629 0, /* Alternatives #. We are not interesting in alternatives
630 because we does not proceed debug_insns for reloads. */
631 NULL, /* Hard registers referenced in machine description. */
632 NULL /* Descriptions of operands in alternatives. */
633 };
634
635 /* The following data are used as static insn data for all debug
636 marker insns. If structure lra_static_insn_data is changed, the
637 initializer should be changed too. */
638 static struct lra_static_insn_data debug_marker_static_data =
639 {
640 &debug_operand_data,
641 0, /* Duplication operands #. */
642 -1, /* Commutative operand #. */
643 0, /* Operands #. There isn't any operand. */
644 0, /* Duplications #. */
645 0, /* Alternatives #. We are not interesting in alternatives
646 because we does not proceed debug_insns for reloads. */
647 NULL, /* Hard registers referenced in machine description. */
648 NULL /* Descriptions of operands in alternatives. */
649 };
650
651 /* Called once per compiler work to initialize some LRA data related
652 to insns. */
653 static void
654 init_insn_code_data_once (void)
655 {
656 memset (insn_code_data, 0, sizeof (insn_code_data));
657 }
658
659 /* Called once per compiler work to finalize some LRA data related to
660 insns. */
661 static void
662 finish_insn_code_data_once (void)
663 {
664 for (unsigned int i = 0; i < NUM_INSN_CODES; i++)
665 {
666 if (insn_code_data[i] != NULL)
667 {
668 free (insn_code_data[i]);
669 insn_code_data[i] = NULL;
670 }
671 }
672 }
673
674 /* Return static insn data, allocate and setup if necessary. Although
675 dup_num is static data (it depends only on icode), to set it up we
676 need to extract insn first. So recog_data should be valid for
677 normal insn (ICODE >= 0) before the call. */
678 static struct lra_static_insn_data *
679 get_static_insn_data (int icode, int nop, int ndup, int nalt)
680 {
681 struct lra_static_insn_data *data;
682 size_t n_bytes;
683
684 lra_assert (icode < (int) NUM_INSN_CODES);
685 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
686 return data;
687 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
688 n_bytes = sizeof (struct lra_static_insn_data)
689 + sizeof (struct lra_operand_data) * nop
690 + sizeof (int) * ndup;
691 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
692 data->operand_alternative = NULL;
693 data->n_operands = nop;
694 data->n_dups = ndup;
695 data->n_alternatives = nalt;
696 data->operand = ((struct lra_operand_data *)
697 ((char *) data + sizeof (struct lra_static_insn_data)));
698 data->dup_num = ((int *) ((char *) data->operand
699 + sizeof (struct lra_operand_data) * nop));
700 if (icode >= 0)
701 {
702 int i;
703
704 insn_code_data[icode] = data;
705 for (i = 0; i < nop; i++)
706 {
707 data->operand[i].constraint
708 = insn_data[icode].operand[i].constraint;
709 data->operand[i].mode = insn_data[icode].operand[i].mode;
710 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
711 data->operand[i].is_operator
712 = insn_data[icode].operand[i].is_operator;
713 data->operand[i].type
714 = (data->operand[i].constraint[0] == '=' ? OP_OUT
715 : data->operand[i].constraint[0] == '+' ? OP_INOUT
716 : OP_IN);
717 data->operand[i].is_address = false;
718 }
719 for (i = 0; i < ndup; i++)
720 data->dup_num[i] = recog_data.dup_num[i];
721 }
722 return data;
723 }
724
725 /* The current length of the following array. */
726 int lra_insn_recog_data_len;
727
728 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
729 lra_insn_recog_data_t *lra_insn_recog_data;
730
731 /* Alloc pool we allocate entries for lra_insn_recog_data from. */
732 static object_allocator<class lra_insn_recog_data>
733 lra_insn_recog_data_pool ("insn recog data pool");
734
735 /* Initialize LRA data about insns. */
736 static void
737 init_insn_recog_data (void)
738 {
739 lra_insn_recog_data_len = 0;
740 lra_insn_recog_data = NULL;
741 }
742
743 /* Expand, if necessary, LRA data about insns. */
744 static void
745 check_and_expand_insn_recog_data (int index)
746 {
747 int i, old;
748
749 if (lra_insn_recog_data_len > index)
750 return;
751 old = lra_insn_recog_data_len;
752 lra_insn_recog_data_len = index * 3 / 2 + 1;
753 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
754 lra_insn_recog_data,
755 lra_insn_recog_data_len);
756 for (i = old; i < lra_insn_recog_data_len; i++)
757 lra_insn_recog_data[i] = NULL;
758 }
759
760 /* Finish LRA DATA about insn. */
761 static void
762 free_insn_recog_data (lra_insn_recog_data_t data)
763 {
764 if (data->operand_loc != NULL)
765 free (data->operand_loc);
766 if (data->dup_loc != NULL)
767 free (data->dup_loc);
768 if (data->arg_hard_regs != NULL)
769 free (data->arg_hard_regs);
770 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
771 {
772 if (data->insn_static_data->operand_alternative != NULL)
773 free (const_cast <operand_alternative *>
774 (data->insn_static_data->operand_alternative));
775 free_insn_regs (data->insn_static_data->hard_regs);
776 free (data->insn_static_data);
777 }
778 free_insn_regs (data->regs);
779 data->regs = NULL;
780 lra_insn_recog_data_pool.remove (data);
781 }
782
783 /* Pools for copies. */
784 static object_allocator<lra_copy> lra_copy_pool ("lra copies");
785
786 /* Finish LRA data about all insns. */
787 static void
788 finish_insn_recog_data (void)
789 {
790 int i;
791 lra_insn_recog_data_t data;
792
793 for (i = 0; i < lra_insn_recog_data_len; i++)
794 if ((data = lra_insn_recog_data[i]) != NULL)
795 free_insn_recog_data (data);
796 finish_insn_regs ();
797 lra_copy_pool.release ();
798 lra_insn_reg_pool.release ();
799 lra_insn_recog_data_pool.release ();
800 free (lra_insn_recog_data);
801 }
802
803 /* Setup info about operands in alternatives of LRA DATA of insn. */
804 static void
805 setup_operand_alternative (lra_insn_recog_data_t data,
806 const operand_alternative *op_alt)
807 {
808 int i, j, nop, nalt;
809 int icode = data->icode;
810 struct lra_static_insn_data *static_data = data->insn_static_data;
811
812 static_data->commutative = -1;
813 nop = static_data->n_operands;
814 nalt = static_data->n_alternatives;
815 static_data->operand_alternative = op_alt;
816 for (i = 0; i < nop; i++)
817 {
818 static_data->operand[i].early_clobber_alts = 0;
819 static_data->operand[i].is_address = false;
820 if (static_data->operand[i].constraint[0] == '%')
821 {
822 /* We currently only support one commutative pair of operands. */
823 if (static_data->commutative < 0)
824 static_data->commutative = i;
825 else
826 lra_assert (icode < 0); /* Asm */
827 /* The last operand should not be marked commutative. */
828 lra_assert (i != nop - 1);
829 }
830 }
831 for (j = 0; j < nalt; j++)
832 for (i = 0; i < nop; i++, op_alt++)
833 {
834 if (op_alt->earlyclobber)
835 static_data->operand[i].early_clobber_alts |= (alternative_mask) 1 << j;
836 static_data->operand[i].is_address |= op_alt->is_address;
837 }
838 }
839
840 /* Recursively process X and collect info about registers, which are
841 not the insn operands, in X with TYPE (in/out/inout) and flag that
842 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
843 to LIST. X is a part of insn given by DATA. Return the result
844 list. */
845 static struct lra_insn_reg *
846 collect_non_operand_hard_regs (rtx_insn *insn, rtx *x,
847 lra_insn_recog_data_t data,
848 struct lra_insn_reg *list,
849 enum op_type type, bool early_clobber)
850 {
851 int i, j, regno, last;
852 bool subreg_p;
853 machine_mode mode;
854 struct lra_insn_reg *curr;
855 rtx op = *x;
856 enum rtx_code code = GET_CODE (op);
857 const char *fmt = GET_RTX_FORMAT (code);
858
859 for (i = 0; i < data->insn_static_data->n_operands; i++)
860 if (! data->insn_static_data->operand[i].is_operator
861 && x == data->operand_loc[i])
862 /* It is an operand loc. Stop here. */
863 return list;
864 for (i = 0; i < data->insn_static_data->n_dups; i++)
865 if (x == data->dup_loc[i])
866 /* It is a dup loc. Stop here. */
867 return list;
868 mode = GET_MODE (op);
869 subreg_p = false;
870 if (code == SUBREG)
871 {
872 mode = wider_subreg_mode (op);
873 if (read_modify_subreg_p (op))
874 subreg_p = true;
875 op = SUBREG_REG (op);
876 code = GET_CODE (op);
877 }
878 if (REG_P (op))
879 {
880 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
881 return list;
882 /* Process all regs even unallocatable ones as we need info
883 about all regs for rematerialization pass. */
884 for (last = end_hard_regno (mode, regno); regno < last; regno++)
885 {
886 for (curr = list; curr != NULL; curr = curr->next)
887 if (curr->regno == regno && curr->subreg_p == subreg_p
888 && curr->biggest_mode == mode)
889 {
890 if (curr->type != type)
891 curr->type = OP_INOUT;
892 if (early_clobber)
893 curr->early_clobber_alts = ALL_ALTERNATIVES;
894 break;
895 }
896 if (curr == NULL)
897 {
898 /* This is a new hard regno or the info cannot be
899 integrated into the found structure. */
900 #ifdef STACK_REGS
901 early_clobber
902 = (early_clobber
903 /* This clobber is to inform popping floating
904 point stack only. */
905 && ! (FIRST_STACK_REG <= regno
906 && regno <= LAST_STACK_REG));
907 #endif
908 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
909 early_clobber ? ALL_ALTERNATIVES : 0, list);
910 }
911 }
912 return list;
913 }
914 switch (code)
915 {
916 case SET:
917 list = collect_non_operand_hard_regs (insn, &SET_DEST (op), data,
918 list, OP_OUT, false);
919 list = collect_non_operand_hard_regs (insn, &SET_SRC (op), data,
920 list, OP_IN, false);
921 break;
922 case CLOBBER:
923 /* We treat clobber of non-operand hard registers as early clobber. */
924 list = collect_non_operand_hard_regs (insn, &XEXP (op, 0), data,
925 list, OP_OUT, true);
926 break;
927 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
928 list = collect_non_operand_hard_regs (insn, &XEXP (op, 0), data,
929 list, OP_INOUT, false);
930 break;
931 case PRE_MODIFY: case POST_MODIFY:
932 list = collect_non_operand_hard_regs (insn, &XEXP (op, 0), data,
933 list, OP_INOUT, false);
934 list = collect_non_operand_hard_regs (insn, &XEXP (op, 1), data,
935 list, OP_IN, false);
936 break;
937 default:
938 fmt = GET_RTX_FORMAT (code);
939 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
940 {
941 if (fmt[i] == 'e')
942 list = collect_non_operand_hard_regs (insn, &XEXP (op, i), data,
943 list, OP_IN, false);
944 else if (fmt[i] == 'E')
945 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
946 list = collect_non_operand_hard_regs (insn, &XVECEXP (op, i, j),
947 data, list, OP_IN, false);
948 }
949 }
950 return list;
951 }
952
953 /* Set up and return info about INSN. Set up the info if it is not set up
954 yet. */
955 lra_insn_recog_data_t
956 lra_set_insn_recog_data (rtx_insn *insn)
957 {
958 lra_insn_recog_data_t data;
959 int i, n, icode;
960 rtx **locs;
961 unsigned int uid = INSN_UID (insn);
962 struct lra_static_insn_data *insn_static_data;
963
964 check_and_expand_insn_recog_data (uid);
965 if (DEBUG_INSN_P (insn))
966 icode = -1;
967 else
968 {
969 icode = INSN_CODE (insn);
970 if (icode < 0)
971 /* It might be a new simple insn which is not recognized yet. */
972 INSN_CODE (insn) = icode = recog_memoized (insn);
973 }
974 data = lra_insn_recog_data_pool.allocate ();
975 lra_insn_recog_data[uid] = data;
976 data->insn = insn;
977 data->used_insn_alternative = LRA_UNKNOWN_ALT;
978 data->icode = icode;
979 data->regs = NULL;
980 if (DEBUG_INSN_P (insn))
981 {
982 data->dup_loc = NULL;
983 data->arg_hard_regs = NULL;
984 data->preferred_alternatives = ALL_ALTERNATIVES;
985 if (DEBUG_BIND_INSN_P (insn))
986 {
987 data->insn_static_data = &debug_bind_static_data;
988 data->operand_loc = XNEWVEC (rtx *, 1);
989 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
990 }
991 else if (DEBUG_MARKER_INSN_P (insn))
992 {
993 data->insn_static_data = &debug_marker_static_data;
994 data->operand_loc = NULL;
995 }
996 return data;
997 }
998 if (icode < 0)
999 {
1000 int nop, nalt;
1001 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1002 const char *constraints[MAX_RECOG_OPERANDS];
1003
1004 nop = asm_noperands (PATTERN (insn));
1005 data->operand_loc = data->dup_loc = NULL;
1006 nalt = 1;
1007 if (nop < 0)
1008 {
1009 /* It is a special insn like USE or CLOBBER. We should
1010 recognize any regular insn otherwise LRA can do nothing
1011 with this insn. */
1012 gcc_assert (GET_CODE (PATTERN (insn)) == USE
1013 || GET_CODE (PATTERN (insn)) == CLOBBER
1014 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
1015 data->insn_static_data = insn_static_data
1016 = get_static_insn_data (-1, 0, 0, nalt);
1017 }
1018 else
1019 {
1020 /* expand_asm_operands makes sure there aren't too many
1021 operands. */
1022 lra_assert (nop <= MAX_RECOG_OPERANDS);
1023 if (nop != 0)
1024 data->operand_loc = XNEWVEC (rtx *, nop);
1025 /* Now get the operand values and constraints out of the
1026 insn. */
1027 decode_asm_operands (PATTERN (insn), NULL,
1028 data->operand_loc,
1029 constraints, operand_mode, NULL);
1030 if (nop > 0)
1031 for (const char *p =constraints[0]; *p; p++)
1032 nalt += *p == ',';
1033 data->insn_static_data = insn_static_data
1034 = get_static_insn_data (-1, nop, 0, nalt);
1035 for (i = 0; i < nop; i++)
1036 {
1037 insn_static_data->operand[i].mode = operand_mode[i];
1038 insn_static_data->operand[i].constraint = constraints[i];
1039 insn_static_data->operand[i].strict_low = false;
1040 insn_static_data->operand[i].is_operator = false;
1041 insn_static_data->operand[i].is_address = false;
1042 }
1043 }
1044 for (i = 0; i < insn_static_data->n_operands; i++)
1045 insn_static_data->operand[i].type
1046 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1047 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1048 : OP_IN);
1049 data->preferred_alternatives = ALL_ALTERNATIVES;
1050 if (nop > 0)
1051 {
1052 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
1053 nalt * nop);
1054 preprocess_constraints (nop, nalt, constraints, op_alt,
1055 data->operand_loc);
1056 setup_operand_alternative (data, op_alt);
1057 }
1058 }
1059 else
1060 {
1061 insn_extract (insn);
1062 data->insn_static_data = insn_static_data
1063 = get_static_insn_data (icode, insn_data[icode].n_operands,
1064 insn_data[icode].n_dups,
1065 insn_data[icode].n_alternatives);
1066 n = insn_static_data->n_operands;
1067 if (n == 0)
1068 locs = NULL;
1069 else
1070 {
1071 locs = XNEWVEC (rtx *, n);
1072 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1073 }
1074 data->operand_loc = locs;
1075 n = insn_static_data->n_dups;
1076 if (n == 0)
1077 locs = NULL;
1078 else
1079 {
1080 locs = XNEWVEC (rtx *, n);
1081 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1082 }
1083 data->dup_loc = locs;
1084 data->preferred_alternatives = get_preferred_alternatives (insn);
1085 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1086 if (!insn_static_data->operand_alternative)
1087 setup_operand_alternative (data, op_alt);
1088 else if (op_alt != insn_static_data->operand_alternative)
1089 insn_static_data->operand_alternative = op_alt;
1090 }
1091 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1092 insn_static_data->hard_regs = NULL;
1093 else
1094 insn_static_data->hard_regs
1095 = collect_non_operand_hard_regs (insn, &PATTERN (insn), data,
1096 NULL, OP_IN, false);
1097 data->arg_hard_regs = NULL;
1098 if (CALL_P (insn))
1099 {
1100 bool use_p;
1101 rtx link;
1102 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1103
1104 n_hard_regs = 0;
1105 /* Finding implicit hard register usage. We believe it will be
1106 not changed whatever transformations are used. Call insns
1107 are such example. */
1108 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1109 link != NULL_RTX;
1110 link = XEXP (link, 1))
1111 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1112 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1113 && REG_P (XEXP (XEXP (link, 0), 0)))
1114 {
1115 regno = REGNO (XEXP (XEXP (link, 0), 0));
1116 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1117 /* It is an argument register. */
1118 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1119 arg_hard_regs[n_hard_regs++]
1120 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
1121 }
1122
1123 if (n_hard_regs != 0)
1124 {
1125 arg_hard_regs[n_hard_regs++] = -1;
1126 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1127 memcpy (data->arg_hard_regs, arg_hard_regs,
1128 sizeof (int) * n_hard_regs);
1129 }
1130 }
1131 /* Some output operand can be recognized only from the context not
1132 from the constraints which are empty in this case. Call insn may
1133 contain a hard register in set destination with empty constraint
1134 and extract_insn treats them as an input. */
1135 for (i = 0; i < insn_static_data->n_operands; i++)
1136 {
1137 int j;
1138 rtx pat, set;
1139 struct lra_operand_data *operand = &insn_static_data->operand[i];
1140
1141 /* ??? Should we treat 'X' the same way. It looks to me that
1142 'X' means anything and empty constraint means we do not
1143 care. */
1144 if (operand->type != OP_IN || *operand->constraint != '\0'
1145 || operand->is_operator)
1146 continue;
1147 pat = PATTERN (insn);
1148 if (GET_CODE (pat) == SET)
1149 {
1150 if (data->operand_loc[i] != &SET_DEST (pat))
1151 continue;
1152 }
1153 else if (GET_CODE (pat) == PARALLEL)
1154 {
1155 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1156 {
1157 set = XVECEXP (PATTERN (insn), 0, j);
1158 if (GET_CODE (set) == SET
1159 && &SET_DEST (set) == data->operand_loc[i])
1160 break;
1161 }
1162 if (j < 0)
1163 continue;
1164 }
1165 else
1166 continue;
1167 operand->type = OP_OUT;
1168 }
1169 return data;
1170 }
1171
1172 /* Return info about insn give by UID. The info should be already set
1173 up. */
1174 static lra_insn_recog_data_t
1175 get_insn_recog_data_by_uid (int uid)
1176 {
1177 lra_insn_recog_data_t data;
1178
1179 data = lra_insn_recog_data[uid];
1180 lra_assert (data != NULL);
1181 return data;
1182 }
1183
1184 /* Invalidate all info about insn given by its UID. */
1185 static void
1186 invalidate_insn_recog_data (int uid)
1187 {
1188 lra_insn_recog_data_t data;
1189
1190 data = lra_insn_recog_data[uid];
1191 lra_assert (data != NULL);
1192 free_insn_recog_data (data);
1193 lra_insn_recog_data[uid] = NULL;
1194 }
1195
1196 /* Update all the insn info about INSN. It is usually called when
1197 something in the insn was changed. Return the updated info. */
1198 lra_insn_recog_data_t
1199 lra_update_insn_recog_data (rtx_insn *insn)
1200 {
1201 lra_insn_recog_data_t data;
1202 int n;
1203 unsigned int uid = INSN_UID (insn);
1204 struct lra_static_insn_data *insn_static_data;
1205 poly_int64 sp_offset = 0;
1206
1207 check_and_expand_insn_recog_data (uid);
1208 if ((data = lra_insn_recog_data[uid]) != NULL
1209 && data->icode != INSN_CODE (insn))
1210 {
1211 sp_offset = data->sp_offset;
1212 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1213 invalidate_insn_recog_data (uid);
1214 data = NULL;
1215 }
1216 if (data == NULL)
1217 {
1218 data = lra_get_insn_recog_data (insn);
1219 /* Initiate or restore SP offset. */
1220 data->sp_offset = sp_offset;
1221 return data;
1222 }
1223 insn_static_data = data->insn_static_data;
1224 data->used_insn_alternative = LRA_UNKNOWN_ALT;
1225 if (DEBUG_INSN_P (insn))
1226 return data;
1227 if (data->icode < 0)
1228 {
1229 int nop;
1230 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1231 const char *constraints[MAX_RECOG_OPERANDS];
1232
1233 nop = asm_noperands (PATTERN (insn));
1234 if (nop >= 0)
1235 {
1236 lra_assert (nop == data->insn_static_data->n_operands);
1237 /* Now get the operand values and constraints out of the
1238 insn. */
1239 decode_asm_operands (PATTERN (insn), NULL,
1240 data->operand_loc,
1241 constraints, operand_mode, NULL);
1242
1243 if (flag_checking)
1244 for (int i = 0; i < nop; i++)
1245 lra_assert
1246 (insn_static_data->operand[i].mode == operand_mode[i]
1247 && insn_static_data->operand[i].constraint == constraints[i]
1248 && ! insn_static_data->operand[i].is_operator);
1249 }
1250
1251 if (flag_checking)
1252 for (int i = 0; i < insn_static_data->n_operands; i++)
1253 lra_assert
1254 (insn_static_data->operand[i].type
1255 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1256 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1257 : OP_IN));
1258 }
1259 else
1260 {
1261 insn_extract (insn);
1262 n = insn_static_data->n_operands;
1263 if (n != 0)
1264 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1265 n = insn_static_data->n_dups;
1266 if (n != 0)
1267 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1268 lra_assert (check_bool_attrs (insn));
1269 }
1270 return data;
1271 }
1272
1273 /* Set up that INSN is using alternative ALT now. */
1274 void
1275 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1276 {
1277 lra_insn_recog_data_t data;
1278
1279 data = lra_get_insn_recog_data (insn);
1280 data->used_insn_alternative = alt;
1281 }
1282
1283 /* Set up that insn with UID is using alternative ALT now. The insn
1284 info should be already set up. */
1285 void
1286 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1287 {
1288 lra_insn_recog_data_t data;
1289
1290 check_and_expand_insn_recog_data (uid);
1291 data = lra_insn_recog_data[uid];
1292 lra_assert (data != NULL);
1293 data->used_insn_alternative = alt;
1294 }
1295
1296
1297
1299 /* This page contains code dealing with common register info and
1300 pseudo copies. */
1301
1302 /* The size of the following array. */
1303 static int reg_info_size;
1304 /* Common info about each register. */
1305 class lra_reg *lra_reg_info;
1306
1307 HARD_REG_SET hard_regs_spilled_into;
1308
1309 /* Last register value. */
1310 static int last_reg_value;
1311
1312 /* Return new register value. */
1313 static int
1314 get_new_reg_value (void)
1315 {
1316 return ++last_reg_value;
1317 }
1318
1319 /* Vec referring to pseudo copies. */
1320 static vec<lra_copy_t> copy_vec;
1321
1322 /* Initialize I-th element of lra_reg_info. */
1323 static inline void
1324 initialize_lra_reg_info_element (int i)
1325 {
1326 bitmap_initialize (&lra_reg_info[i].insn_bitmap, ®_obstack);
1327 #ifdef STACK_REGS
1328 lra_reg_info[i].no_stack_p = false;
1329 #endif
1330 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1331 CLEAR_HARD_REG_SET (lra_reg_info[i].exclude_start_hard_regs);
1332 lra_reg_info[i].preferred_hard_regno1 = -1;
1333 lra_reg_info[i].preferred_hard_regno2 = -1;
1334 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1335 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1336 lra_reg_info[i].biggest_mode = VOIDmode;
1337 lra_reg_info[i].live_ranges = NULL;
1338 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1339 lra_reg_info[i].last_reload = 0;
1340 lra_reg_info[i].restore_rtx = NULL_RTX;
1341 lra_reg_info[i].val = get_new_reg_value ();
1342 lra_reg_info[i].offset = 0;
1343 lra_reg_info[i].copies = NULL;
1344 }
1345
1346 /* Initialize common reg info and copies. */
1347 static void
1348 init_reg_info (void)
1349 {
1350 int i;
1351
1352 last_reg_value = 0;
1353 reg_info_size = max_reg_num () * 3 / 2 + 1;
1354 lra_reg_info = XNEWVEC (class lra_reg, reg_info_size);
1355 for (i = 0; i < reg_info_size; i++)
1356 initialize_lra_reg_info_element (i);
1357 copy_vec.truncate (0);
1358 CLEAR_HARD_REG_SET (hard_regs_spilled_into);
1359 }
1360
1361
1362 /* Finish common reg info and copies. */
1363 static void
1364 finish_reg_info (void)
1365 {
1366 int i;
1367
1368 for (i = 0; i < reg_info_size; i++)
1369 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1370 free (lra_reg_info);
1371 reg_info_size = 0;
1372 }
1373
1374 /* Expand common reg info if it is necessary. */
1375 static void
1376 expand_reg_info (void)
1377 {
1378 int i, old = reg_info_size;
1379
1380 if (reg_info_size > max_reg_num ())
1381 return;
1382 reg_info_size = max_reg_num () * 3 / 2 + 1;
1383 lra_reg_info = XRESIZEVEC (class lra_reg, lra_reg_info, reg_info_size);
1384 for (i = old; i < reg_info_size; i++)
1385 initialize_lra_reg_info_element (i);
1386 }
1387
1388 /* Free all copies. */
1389 void
1390 lra_free_copies (void)
1391 {
1392 lra_copy_t cp;
1393
1394 while (copy_vec.length () != 0)
1395 {
1396 cp = copy_vec.pop ();
1397 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1398 lra_copy_pool.remove (cp);
1399 }
1400 }
1401
1402 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1403 frequency is FREQ. */
1404 void
1405 lra_create_copy (int regno1, int regno2, int freq)
1406 {
1407 bool regno1_dest_p;
1408 lra_copy_t cp;
1409
1410 lra_assert (regno1 != regno2);
1411 regno1_dest_p = true;
1412 if (regno1 > regno2)
1413 {
1414 std::swap (regno1, regno2);
1415 regno1_dest_p = false;
1416 }
1417 cp = lra_copy_pool.allocate ();
1418 copy_vec.safe_push (cp);
1419 cp->regno1_dest_p = regno1_dest_p;
1420 cp->freq = freq;
1421 cp->regno1 = regno1;
1422 cp->regno2 = regno2;
1423 cp->regno1_next = lra_reg_info[regno1].copies;
1424 lra_reg_info[regno1].copies = cp;
1425 cp->regno2_next = lra_reg_info[regno2].copies;
1426 lra_reg_info[regno2].copies = cp;
1427 if (lra_dump_file != NULL)
1428 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1429 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1430 }
1431
1432 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1433 NULL. */
1434 lra_copy_t
1435 lra_get_copy (int n)
1436 {
1437 if (n >= (int) copy_vec.length ())
1438 return NULL;
1439 return copy_vec[n];
1440 }
1441
1442
1443
1445 /* This page contains code dealing with info about registers in
1446 insns. */
1447
1448 /* Process X of INSN recursively and add info (operand type is given
1449 by TYPE) about registers in X to the insn DATA. If X can be early
1450 clobbered, alternatives in which it can be early clobbered are given
1451 by EARLY_CLOBBER_ALTS. */
1452 static void
1453 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x,
1454 rtx_insn *insn, enum op_type type,
1455 alternative_mask early_clobber_alts)
1456 {
1457 int i, j, regno;
1458 bool subreg_p;
1459 machine_mode mode;
1460 const char *fmt;
1461 enum rtx_code code;
1462 struct lra_insn_reg *curr;
1463
1464 code = GET_CODE (x);
1465 mode = GET_MODE (x);
1466 subreg_p = false;
1467 if (GET_CODE (x) == SUBREG)
1468 {
1469 mode = wider_subreg_mode (x);
1470 if (read_modify_subreg_p (x))
1471 subreg_p = true;
1472 x = SUBREG_REG (x);
1473 code = GET_CODE (x);
1474 }
1475 if (REG_P (x))
1476 {
1477 regno = REGNO (x);
1478 /* Process all regs even unallocatable ones as we need info about
1479 all regs for rematerialization pass. */
1480 expand_reg_info ();
1481 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, INSN_UID (insn)))
1482 {
1483 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1484 early_clobber_alts, data->regs);
1485 return;
1486 }
1487 else
1488 {
1489 for (curr = data->regs; curr != NULL; curr = curr->next)
1490 if (curr->regno == regno)
1491 {
1492 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1493 /* The info cannot be integrated into the found
1494 structure. */
1495 data->regs = new_insn_reg (data->insn, regno, type, mode,
1496 subreg_p, early_clobber_alts,
1497 data->regs);
1498 else
1499 {
1500 if (curr->type != type)
1501 curr->type = OP_INOUT;
1502 curr->early_clobber_alts |= early_clobber_alts;
1503 }
1504 return;
1505 }
1506 gcc_unreachable ();
1507 }
1508 }
1509
1510 switch (code)
1511 {
1512 case SET:
1513 add_regs_to_insn_regno_info (data, SET_DEST (x), insn, OP_OUT, 0);
1514 add_regs_to_insn_regno_info (data, SET_SRC (x), insn, OP_IN, 0);
1515 break;
1516 case CLOBBER:
1517 /* We treat clobber of non-operand hard registers as early
1518 clobber. */
1519 add_regs_to_insn_regno_info (data, XEXP (x, 0), insn, OP_OUT,
1520 ALL_ALTERNATIVES);
1521 break;
1522 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1523 add_regs_to_insn_regno_info (data, XEXP (x, 0), insn, OP_INOUT, 0);
1524 break;
1525 case PRE_MODIFY: case POST_MODIFY:
1526 add_regs_to_insn_regno_info (data, XEXP (x, 0), insn, OP_INOUT, 0);
1527 add_regs_to_insn_regno_info (data, XEXP (x, 1), insn, OP_IN, 0);
1528 break;
1529 default:
1530 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1531 /* Some targets place small structures in registers for return
1532 values of functions, and those registers are wrapped in
1533 PARALLEL that we may see as the destination of a SET. Here
1534 is an example:
1535
1536 (call_insn 13 12 14 2 (set (parallel:BLK [
1537 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1538 (const_int 0 [0]))
1539 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1540 (const_int 8 [0x8]))
1541 ])
1542 (call (mem:QI (symbol_ref:DI (... */
1543 type = OP_IN;
1544 fmt = GET_RTX_FORMAT (code);
1545 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1546 {
1547 if (fmt[i] == 'e')
1548 add_regs_to_insn_regno_info (data, XEXP (x, i), insn, type, 0);
1549 else if (fmt[i] == 'E')
1550 {
1551 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1552 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), insn,
1553 type, 0);
1554 }
1555 }
1556 }
1557 }
1558
1559 /* Return execution frequency of INSN. */
1560 static int
1561 get_insn_freq (rtx_insn *insn)
1562 {
1563 basic_block bb = BLOCK_FOR_INSN (insn);
1564
1565 gcc_checking_assert (bb != NULL);
1566 return REG_FREQ_FROM_BB (bb);
1567 }
1568
1569 /* Invalidate all reg info of INSN with DATA and execution frequency
1570 FREQ. Update common info about the invalidated registers. */
1571 static void
1572 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1573 int freq)
1574 {
1575 int uid;
1576 bool debug_p;
1577 unsigned int i;
1578 struct lra_insn_reg *ir, *next_ir;
1579
1580 uid = INSN_UID (insn);
1581 debug_p = DEBUG_INSN_P (insn);
1582 for (ir = data->regs; ir != NULL; ir = next_ir)
1583 {
1584 i = ir->regno;
1585 next_ir = ir->next;
1586 lra_insn_reg_pool.remove (ir);
1587 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1588 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1589 {
1590 lra_reg_info[i].nrefs--;
1591 lra_reg_info[i].freq -= freq;
1592 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1593 }
1594 }
1595 data->regs = NULL;
1596 }
1597
1598 /* Invalidate all reg info of INSN. Update common info about the
1599 invalidated registers. */
1600 void
1601 lra_invalidate_insn_regno_info (rtx_insn *insn)
1602 {
1603 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1604 get_insn_freq (insn));
1605 }
1606
1607 /* Update common reg info from reg info of insn given by its DATA and
1608 execution frequency FREQ. */
1609 static void
1610 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1611 {
1612 unsigned int i;
1613 struct lra_insn_reg *ir;
1614
1615 for (ir = data->regs; ir != NULL; ir = ir->next)
1616 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1617 {
1618 lra_reg_info[i].nrefs++;
1619 lra_reg_info[i].freq += freq;
1620 }
1621 }
1622
1623 /* Set up insn reg info of INSN. Update common reg info from reg info
1624 of INSN. */
1625 void
1626 lra_update_insn_regno_info (rtx_insn *insn)
1627 {
1628 int i, freq;
1629 lra_insn_recog_data_t data;
1630 struct lra_static_insn_data *static_data;
1631 enum rtx_code code;
1632 rtx link;
1633
1634 if (! INSN_P (insn))
1635 return;
1636 data = lra_get_insn_recog_data (insn);
1637 static_data = data->insn_static_data;
1638 freq = NONDEBUG_INSN_P (insn) ? get_insn_freq (insn) : 0;
1639 invalidate_insn_data_regno_info (data, insn, freq);
1640 for (i = static_data->n_operands - 1; i >= 0; i--)
1641 add_regs_to_insn_regno_info (data, *data->operand_loc[i], insn,
1642 static_data->operand[i].type,
1643 static_data->operand[i].early_clobber_alts);
1644 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1645 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), insn,
1646 code == USE ? OP_IN : OP_OUT, 0);
1647 if (CALL_P (insn))
1648 /* On some targets call insns can refer to pseudos in memory in
1649 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1650 consider their occurrences in calls for different
1651 transformations (e.g. inheritance) with given pseudos. */
1652 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1653 link != NULL_RTX;
1654 link = XEXP (link, 1))
1655 {
1656 code = GET_CODE (XEXP (link, 0));
1657 if ((code == USE || code == CLOBBER)
1658 && MEM_P (XEXP (XEXP (link, 0), 0)))
1659 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), insn,
1660 code == USE ? OP_IN : OP_OUT, 0);
1661 }
1662 if (NONDEBUG_INSN_P (insn))
1663 setup_insn_reg_info (data, freq);
1664 }
1665
1666 /* Return reg info of insn given by it UID. */
1667 struct lra_insn_reg *
1668 lra_get_insn_regs (int uid)
1669 {
1670 lra_insn_recog_data_t data;
1671
1672 data = get_insn_recog_data_by_uid (uid);
1673 return data->regs;
1674 }
1675
1676
1677
1679 /* Recursive hash function for RTL X. */
1680 hashval_t
1681 lra_rtx_hash (rtx x)
1682 {
1683 int i, j;
1684 enum rtx_code code;
1685 const char *fmt;
1686 hashval_t val = 0;
1687
1688 if (x == 0)
1689 return val;
1690
1691 code = GET_CODE (x);
1692 val += (int) code + 4095;
1693
1694 /* Some RTL can be compared nonrecursively. */
1695 switch (code)
1696 {
1697 case REG:
1698 return val + REGNO (x);
1699
1700 case LABEL_REF:
1701 return iterative_hash_object (XEXP (x, 0), val);
1702
1703 case SYMBOL_REF:
1704 return iterative_hash_object (XSTR (x, 0), val);
1705
1706 case SCRATCH:
1707 case CONST_DOUBLE:
1708 case CONST_VECTOR:
1709 return val;
1710
1711 case CONST_INT:
1712 return val + UINTVAL (x);
1713
1714 default:
1715 break;
1716 }
1717
1718 /* Hash the elements. */
1719 fmt = GET_RTX_FORMAT (code);
1720 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1721 {
1722 switch (fmt[i])
1723 {
1724 case 'w':
1725 val += XWINT (x, i);
1726 break;
1727
1728 case 'n':
1729 case 'i':
1730 val += XINT (x, i);
1731 break;
1732
1733 case 'V':
1734 case 'E':
1735 val += XVECLEN (x, i);
1736
1737 for (j = 0; j < XVECLEN (x, i); j++)
1738 val += lra_rtx_hash (XVECEXP (x, i, j));
1739 break;
1740
1741 case 'e':
1742 val += lra_rtx_hash (XEXP (x, i));
1743 break;
1744
1745 case 'S':
1746 case 's':
1747 val += htab_hash_string (XSTR (x, i));
1748 break;
1749
1750 case 'u':
1751 case '0':
1752 case 't':
1753 break;
1754
1755 /* It is believed that rtx's at this level will never
1756 contain anything but integers and other rtx's, except for
1757 within LABEL_REFs and SYMBOL_REFs. */
1758 default:
1759 abort ();
1760 }
1761 }
1762 return val;
1763 }
1764
1765
1766
1768 /* This page contains code dealing with stack of the insns which
1769 should be processed by the next constraint pass. */
1770
1771 /* Bitmap used to put an insn on the stack only in one exemplar. */
1772 static sbitmap lra_constraint_insn_stack_bitmap;
1773
1774 /* The stack itself. */
1775 vec<rtx_insn *> lra_constraint_insn_stack;
1776
1777 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1778 info for INSN, otherwise only update it if INSN is not already on the
1779 stack. */
1780 static inline void
1781 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1782 {
1783 unsigned int uid = INSN_UID (insn);
1784 if (always_update)
1785 lra_update_insn_regno_info (insn);
1786 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1787 lra_constraint_insn_stack_bitmap =
1788 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1789 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1790 return;
1791 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1792 if (! always_update)
1793 lra_update_insn_regno_info (insn);
1794 lra_constraint_insn_stack.safe_push (insn);
1795 }
1796
1797 /* Put INSN on the stack. */
1798 void
1799 lra_push_insn (rtx_insn *insn)
1800 {
1801 lra_push_insn_1 (insn, false);
1802 }
1803
1804 /* Put INSN on the stack and update its reg info. */
1805 void
1806 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1807 {
1808 lra_push_insn_1 (insn, true);
1809 }
1810
1811 /* Put insn with UID on the stack. */
1812 void
1813 lra_push_insn_by_uid (unsigned int uid)
1814 {
1815 lra_push_insn (lra_insn_recog_data[uid]->insn);
1816 }
1817
1818 /* Take the last-inserted insns off the stack and return it. */
1819 rtx_insn *
1820 lra_pop_insn (void)
1821 {
1822 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1823 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1824 return insn;
1825 }
1826
1827 /* Return the current size of the insn stack. */
1828 unsigned int
1829 lra_insn_stack_length (void)
1830 {
1831 return lra_constraint_insn_stack.length ();
1832 }
1833
1834 /* Push insns FROM to TO (excluding it) going in reverse order. */
1835 static void
1836 push_insns (rtx_insn *from, rtx_insn *to)
1837 {
1838 rtx_insn *insn;
1839
1840 if (from == NULL_RTX)
1841 return;
1842 for (insn = from; insn != to; insn = PREV_INSN (insn))
1843 if (INSN_P (insn))
1844 lra_push_insn (insn);
1845 }
1846
1847 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1848 taken from the next BB insn after LAST or zero if there in such
1849 insn. */
1850 static void
1851 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1852 {
1853 rtx_insn *before = next_nonnote_nondebug_insn_bb (last);
1854 poly_int64 offset = (before == NULL_RTX || ! INSN_P (before)
1855 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1856
1857 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1858 lra_get_insn_recog_data (insn)->sp_offset = offset;
1859 }
1860
1861 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1862 insns onto the stack. Print about emitting the insns with
1863 TITLE. */
1864 void
1865 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1866 const char *title)
1867 {
1868 if (before == NULL_RTX && after == NULL_RTX)
1869 return;
1870 if (lra_dump_file != NULL)
1871 {
1872 dump_insn_slim (lra_dump_file, insn);
1873 if (before != NULL_RTX)
1874 {
1875 fprintf (lra_dump_file," %s before:\n", title);
1876 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1877 }
1878 }
1879 if (before != NULL_RTX)
1880 {
1881 if (cfun->can_throw_non_call_exceptions)
1882 copy_reg_eh_region_note_forward (insn, before, NULL);
1883 emit_insn_before (before, insn);
1884 push_insns (PREV_INSN (insn), PREV_INSN (before));
1885 setup_sp_offset (before, PREV_INSN (insn));
1886 }
1887 if (after != NULL_RTX)
1888 {
1889 if (cfun->can_throw_non_call_exceptions)
1890 copy_reg_eh_region_note_forward (insn, after, NULL);
1891 if (! JUMP_P (insn))
1892 {
1893 rtx_insn *last;
1894
1895 if (lra_dump_file != NULL)
1896 {
1897 fprintf (lra_dump_file, " %s after:\n", title);
1898 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1899 }
1900 for (last = after;
1901 NEXT_INSN (last) != NULL_RTX;
1902 last = NEXT_INSN (last))
1903 ;
1904 emit_insn_after (after, insn);
1905 push_insns (last, insn);
1906 setup_sp_offset (after, last);
1907 }
1908 else
1909 {
1910 /* Put output reload insns on successor BBs: */
1911 edge_iterator ei;
1912 edge e;
1913
1914 FOR_EACH_EDGE (e, ei, BLOCK_FOR_INSN (insn)->succs)
1915 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1916 {
1917 /* We already made the edge no-critical in ira.cc::ira */
1918 lra_assert (!EDGE_CRITICAL_P (e));
1919 rtx_insn *curr, *tmp = BB_HEAD (e->dest);
1920 if (LABEL_P (tmp))
1921 tmp = NEXT_INSN (tmp);
1922 if (NOTE_INSN_BASIC_BLOCK_P (tmp))
1923 tmp = NEXT_INSN (tmp);
1924 /* Do not put reload insns if it is the last BB
1925 without actual insns. */
1926 if (tmp == NULL)
1927 continue;
1928 start_sequence ();
1929 for (curr = after; curr != NULL_RTX; curr = NEXT_INSN (curr))
1930 emit_insn (copy_insn (PATTERN (curr)));
1931 rtx_insn *copy = get_insns (), *last = get_last_insn ();
1932 end_sequence ();
1933 if (lra_dump_file != NULL)
1934 {
1935 fprintf (lra_dump_file, " %s after in bb%d:\n", title,
1936 e->dest->index);
1937 dump_rtl_slim (lra_dump_file, copy, NULL, -1, 0);
1938 }
1939 /* Use the right emit func for setting up BB_END/BB_HEAD: */
1940 if (BB_END (e->dest) == PREV_INSN (tmp))
1941 emit_insn_after_noloc (copy, PREV_INSN (tmp), e->dest);
1942 else
1943 emit_insn_before_noloc (copy, tmp, e->dest);
1944 push_insns (last, PREV_INSN (copy));
1945 setup_sp_offset (copy, last);
1946 /* We can ignore BB live info here as it and reg notes
1947 will be updated before the next assignment
1948 sub-pass. */
1949 }
1950 }
1951 }
1952 if (lra_dump_file != NULL)
1953 fprintf (lra_dump_file, "\n");
1954 if (cfun->can_throw_non_call_exceptions)
1955 {
1956 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1957 if (note && !insn_could_throw_p (insn))
1958 remove_note (insn, note);
1959 }
1960 }
1961
1962
1964 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1965 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1966 DEBUG_P is if LOC is within a DEBUG_INSN. Return true if any
1967 change was made. */
1968 bool
1969 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p,
1970 bool debug_p)
1971 {
1972 rtx x = *loc;
1973 bool result = false;
1974 enum rtx_code code;
1975 const char *fmt;
1976 int i, j;
1977
1978 if (x == NULL_RTX)
1979 return false;
1980
1981 code = GET_CODE (x);
1982 if (code == SUBREG && subreg_p)
1983 {
1984 rtx subst, inner = SUBREG_REG (x);
1985 /* Transform subreg of constant while we still have inner mode
1986 of the subreg. The subreg internal should not be an insn
1987 operand. */
1988 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1989 && CONSTANT_P (new_reg)
1990 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1991 SUBREG_BYTE (x))) != NULL_RTX)
1992 {
1993 *loc = subst;
1994 return true;
1995 }
1996
1997 }
1998 else if (code == REG && (int) REGNO (x) == old_regno)
1999 {
2000 machine_mode mode = GET_MODE (x);
2001 machine_mode inner_mode = GET_MODE (new_reg);
2002
2003 if (mode != inner_mode
2004 && ! (CONST_SCALAR_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
2005 {
2006 poly_uint64 offset = 0;
2007 if (partial_subreg_p (mode, inner_mode)
2008 && SCALAR_INT_MODE_P (inner_mode))
2009 offset = subreg_lowpart_offset (mode, inner_mode);
2010 if (debug_p)
2011 new_reg = gen_rtx_raw_SUBREG (mode, new_reg, offset);
2012 else
2013 new_reg = gen_rtx_SUBREG (mode, new_reg, offset);
2014 }
2015 *loc = new_reg;
2016 return true;
2017 }
2018
2019 /* Scan all the operand sub-expressions. */
2020 fmt = GET_RTX_FORMAT (code);
2021 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2022 {
2023 if (fmt[i] == 'e')
2024 {
2025 if (debug_p
2026 && i == 0
2027 && (code == SUBREG
2028 || code == ZERO_EXTEND
2029 || code == SIGN_EXTEND
2030 || code == FLOAT
2031 || code == UNSIGNED_FLOAT))
2032 {
2033 rtx y = XEXP (x, 0);
2034 if (lra_substitute_pseudo (&y, old_regno,
2035 new_reg, subreg_p, debug_p))
2036 {
2037 result = true;
2038 if (CONST_SCALAR_INT_P (y))
2039 {
2040 if (code == SUBREG)
2041 y = simplify_subreg (GET_MODE (x), y,
2042 GET_MODE (SUBREG_REG (x)),
2043 SUBREG_BYTE (x));
2044 else
2045 y = simplify_unary_operation (code, GET_MODE (x), y,
2046 GET_MODE (XEXP (x, 0)));
2047 if (y)
2048 *loc = y;
2049 else
2050 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
2051 }
2052 else
2053 XEXP (x, 0) = y;
2054 }
2055 }
2056 else if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
2057 new_reg, subreg_p, debug_p))
2058 result = true;
2059 }
2060 else if (fmt[i] == 'E')
2061 {
2062 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2063 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
2064 new_reg, subreg_p, debug_p))
2065 result = true;
2066 }
2067 }
2068 return result;
2069 }
2070
2071 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
2072 of constant if SUBREG_P. This won't update the insn ptr, just the
2073 contents of the insn. */
2074 bool
2075 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
2076 rtx new_reg, bool subreg_p)
2077 {
2078 rtx loc = insn;
2079 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p,
2080 DEBUG_INSN_P (insn));
2081 }
2082
2083
2084
2086 /* Return new register of the same mode as ORIGINAL of class ALL_REGS.
2087 Used in ira_remove_scratches. */
2088 static rtx
2089 get_scratch_reg (rtx original)
2090 {
2091 return lra_create_new_reg (GET_MODE (original), original, ALL_REGS,
2092 NULL, NULL);
2093 }
2094
2095 /* Remove all insn scratches in INSN. */
2096 static void
2097 remove_insn_scratches (rtx_insn *insn)
2098 {
2099 if (ira_remove_insn_scratches (insn, true, lra_dump_file, get_scratch_reg))
2100 df_insn_rescan (insn);
2101 }
2102
2103 /* Remove all insn scratches in the current function. */
2104 static void
2105 remove_scratches (void)
2106 {
2107 basic_block bb;
2108 rtx_insn *insn;
2109
2110 FOR_EACH_BB_FN (bb, cfun)
2111 FOR_BB_INSNS (bb, insn)
2112 if (INSN_P (insn))
2113 remove_insn_scratches (insn);
2114 }
2115
2116 /* Function checks RTL for correctness. If FINAL_P is true, it is
2117 done at the end of LRA and the check is more rigorous. */
2118 static void
2119 check_rtl (bool final_p)
2120 {
2121 basic_block bb;
2122 rtx_insn *insn;
2123
2124 lra_assert (! final_p || reload_completed);
2125 FOR_EACH_BB_FN (bb, cfun)
2126 FOR_BB_INSNS (bb, insn)
2127 if (NONDEBUG_INSN_P (insn)
2128 && GET_CODE (PATTERN (insn)) != USE
2129 && GET_CODE (PATTERN (insn)) != CLOBBER
2130 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
2131 {
2132 if (final_p)
2133 {
2134 extract_constrain_insn (insn);
2135 continue;
2136 }
2137 /* LRA code is based on assumption that all addresses can be
2138 correctly decomposed. LRA can generate reloads for
2139 decomposable addresses. The decomposition code checks the
2140 correctness of the addresses. So we don't need to check
2141 the addresses here. Don't call insn_invalid_p here, it can
2142 change the code at this stage. */
2143 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2144 fatal_insn_not_found (insn);
2145 }
2146 }
2147
2148 /* Determine if the current function has an exception receiver block
2149 that reaches the exit block via non-exceptional edges */
2150 static bool
2151 has_nonexceptional_receiver (void)
2152 {
2153 edge e;
2154 edge_iterator ei;
2155 basic_block *tos, *worklist, bb;
2156
2157 /* If we're not optimizing, then just err on the safe side. */
2158 if (!optimize)
2159 return true;
2160
2161 /* First determine which blocks can reach exit via normal paths. */
2162 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2163
2164 FOR_EACH_BB_FN (bb, cfun)
2165 bb->flags &= ~BB_REACHABLE;
2166
2167 /* Place the exit block on our worklist. */
2168 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2169 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2170
2171 /* Iterate: find everything reachable from what we've already seen. */
2172 while (tos != worklist)
2173 {
2174 bb = *--tos;
2175
2176 FOR_EACH_EDGE (e, ei, bb->preds)
2177 if (e->flags & EDGE_ABNORMAL)
2178 {
2179 free (worklist);
2180 return true;
2181 }
2182 else
2183 {
2184 basic_block src = e->src;
2185
2186 if (!(src->flags & BB_REACHABLE))
2187 {
2188 src->flags |= BB_REACHABLE;
2189 *tos++ = src;
2190 }
2191 }
2192 }
2193 free (worklist);
2194 /* No exceptional block reached exit unexceptionally. */
2195 return false;
2196 }
2197
2198 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2199 We change pseudos by hard registers without notification of DF and
2200 that can make the notes obsolete. DF-infrastructure does not deal
2201 with REG_INC notes -- so we should regenerate them here. */
2202 static void
2203 update_inc_notes (void)
2204 {
2205 rtx *pnote;
2206 basic_block bb;
2207 rtx_insn *insn;
2208
2209 FOR_EACH_BB_FN (bb, cfun)
2210 FOR_BB_INSNS (bb, insn)
2211 if (NONDEBUG_INSN_P (insn))
2212 {
2213 pnote = ®_NOTES (insn);
2214 while (*pnote != 0)
2215 {
2216 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2217 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2218 || REG_NOTE_KIND (*pnote) == REG_INC)
2219 *pnote = XEXP (*pnote, 1);
2220 else
2221 pnote = &XEXP (*pnote, 1);
2222 }
2223
2224 if (AUTO_INC_DEC)
2225 add_auto_inc_notes (insn, PATTERN (insn));
2226 }
2227 }
2228
2229 /* Set to 1 while in lra. */
2230 int lra_in_progress;
2231
2232 /* Start of pseudo regnos before the LRA. */
2233 int lra_new_regno_start;
2234
2235 /* Start of reload pseudo regnos before the new spill pass. */
2236 int lra_constraint_new_regno_start;
2237
2238 /* Avoid spilling pseudos with regno more than the following value if
2239 it is possible. */
2240 int lra_bad_spill_regno_start;
2241
2242 /* A pseudo of Pmode. */
2243 rtx lra_pmode_pseudo;
2244
2245 /* Inheritance pseudo regnos before the new spill pass. */
2246 bitmap_head lra_inheritance_pseudos;
2247
2248 /* Split regnos before the new spill pass. */
2249 bitmap_head lra_split_regs;
2250
2251 /* Reload pseudo regnos before the new assignment pass which still can
2252 be spilled after the assignment pass as memory is also accepted in
2253 insns for the reload pseudos. */
2254 bitmap_head lra_optional_reload_pseudos;
2255
2256 /* Pseudo regnos used for subreg reloads before the new assignment
2257 pass. Such pseudos still can be spilled after the assignment
2258 pass. */
2259 bitmap_head lra_subreg_reload_pseudos;
2260
2261 /* File used for output of LRA debug information. */
2262 FILE *lra_dump_file;
2263
2264 /* True if we split hard reg after the last constraint sub-pass. */
2265 bool lra_hard_reg_split_p;
2266
2267 /* True if we found an asm error. */
2268 bool lra_asm_error_p;
2269
2270 /* True if we should try spill into registers of different classes
2271 instead of memory. */
2272 bool lra_reg_spill_p;
2273
2274 /* Set up value LRA_REG_SPILL_P. */
2275 static void
2276 setup_reg_spill_flag (void)
2277 {
2278 int cl, mode;
2279
2280 if (targetm.spill_class != NULL)
2281 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2282 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2283 if (targetm.spill_class ((enum reg_class) cl,
2284 (machine_mode) mode) != NO_REGS)
2285 {
2286 lra_reg_spill_p = true;
2287 return;
2288 }
2289 lra_reg_spill_p = false;
2290 }
2291
2292 /* True if the current function is too big to use regular algorithms
2293 in LRA. In other words, we should use simpler and faster algorithms
2294 in LRA. It also means we should not worry about generation code
2295 for caller saves. The value is set up in IRA. */
2296 bool lra_simple_p;
2297
2298 /* Major LRA entry function. F is a file should be used to dump LRA
2299 debug info. */
2300 void
2301 lra (FILE *f)
2302 {
2303 int i;
2304 bool live_p, inserted_p;
2305
2306 lra_dump_file = f;
2307 lra_asm_error_p = false;
2308 lra_pmode_pseudo = gen_reg_rtx (Pmode);
2309
2310 timevar_push (TV_LRA);
2311
2312 /* Make sure that the last insn is a note. Some subsequent passes
2313 need it. */
2314 emit_note (NOTE_INSN_DELETED);
2315
2316 lra_no_alloc_regs = ira_no_alloc_regs;
2317
2318 init_reg_info ();
2319 expand_reg_info ();
2320
2321 init_insn_recog_data ();
2322
2323 /* Some quick check on RTL generated by previous passes. */
2324 if (flag_checking)
2325 check_rtl (false);
2326
2327 lra_in_progress = 1;
2328
2329 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2330 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2331 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2332 lra_rematerialization_iter = 0;
2333
2334 setup_reg_spill_flag ();
2335
2336 /* Function remove_scratches can creates new pseudos for clobbers --
2337 so set up lra_constraint_new_regno_start before its call to
2338 permit changing reg classes for pseudos created by this
2339 simplification. */
2340 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2341 lra_bad_spill_regno_start = INT_MAX;
2342 remove_scratches ();
2343
2344 /* A function that has a non-local label that can reach the exit
2345 block via non-exceptional paths must save all call-saved
2346 registers. */
2347 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2348 crtl->saves_all_registers = 1;
2349
2350 if (crtl->saves_all_registers)
2351 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2352 if (!crtl->abi->clobbers_full_reg_p (i)
2353 && !fixed_regs[i]
2354 && !LOCAL_REGNO (i))
2355 df_set_regs_ever_live (i, true);
2356
2357 /* We don't DF from now and avoid its using because it is to
2358 expensive when a lot of RTL changes are made. */
2359 df_set_flags (DF_NO_INSN_RESCAN);
2360 lra_constraint_insn_stack.create (get_max_uid ());
2361 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2362 bitmap_clear (lra_constraint_insn_stack_bitmap);
2363 lra_live_ranges_init ();
2364 lra_constraints_init ();
2365 lra_curr_reload_num = 0;
2366 push_insns (get_last_insn (), NULL);
2367 /* It is needed for the 1st coalescing. */
2368 bitmap_initialize (&lra_inheritance_pseudos, ®_obstack);
2369 bitmap_initialize (&lra_split_regs, ®_obstack);
2370 bitmap_initialize (&lra_optional_reload_pseudos, ®_obstack);
2371 bitmap_initialize (&lra_subreg_reload_pseudos, ®_obstack);
2372 live_p = false;
2373 if (maybe_ne (get_frame_size (), 0) && crtl->stack_alignment_needed)
2374 /* If we have a stack frame, we must align it now. The stack size
2375 may be a part of the offset computation for register
2376 elimination. */
2377 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2378 lra_init_equiv ();
2379 for (;;)
2380 {
2381 for (;;)
2382 {
2383 bool reloads_p = lra_constraints (lra_constraint_iter == 0);
2384 /* Constraint transformations may result in that eliminable
2385 hard regs become uneliminable and pseudos which use them
2386 should be spilled. It is better to do it before pseudo
2387 assignments.
2388
2389 For example, rs6000 can make
2390 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2391 to use a constant pool. */
2392 lra_eliminate (false, false);
2393 /* We should try to assign hard registers to scratches even
2394 if there were no RTL transformations in lra_constraints.
2395 Also we should check IRA assignments on the first
2396 iteration as they can be wrong because of early clobbers
2397 operands which are ignored in IRA. */
2398 if (! reloads_p && lra_constraint_iter > 1)
2399 {
2400 /* Stack is not empty here only when there are changes
2401 during the elimination sub-pass. */
2402 if (bitmap_empty_p (lra_constraint_insn_stack_bitmap))
2403 break;
2404 else
2405 /* If there are no reloads but changing due
2406 elimination, restart the constraint sub-pass
2407 first. */
2408 continue;
2409 }
2410 /* Do inheritance only for regular algorithms. */
2411 if (! lra_simple_p)
2412 lra_inheritance ();
2413 if (live_p)
2414 lra_clear_live_ranges ();
2415 bool fails_p;
2416 lra_hard_reg_split_p = false;
2417 do
2418 {
2419 /* We need live ranges for lra_assign -- so build them.
2420 But don't remove dead insns or change global live
2421 info as we can undo inheritance transformations after
2422 inheritance pseudo assigning. */
2423 lra_create_live_ranges (true, !lra_simple_p);
2424 live_p = true;
2425 /* If we don't spill non-reload and non-inheritance
2426 pseudos, there is no sense to run memory-memory move
2427 coalescing. If inheritance pseudos were spilled, the
2428 memory-memory moves involving them will be removed by
2429 pass undoing inheritance. */
2430 if (lra_simple_p)
2431 lra_assign (fails_p);
2432 else
2433 {
2434 bool spill_p = !lra_assign (fails_p);
2435
2436 if (lra_undo_inheritance ())
2437 live_p = false;
2438 if (spill_p && ! fails_p)
2439 {
2440 if (! live_p)
2441 {
2442 lra_create_live_ranges (true, true);
2443 live_p = true;
2444 }
2445 if (lra_coalesce ())
2446 live_p = false;
2447 }
2448 if (! live_p)
2449 lra_clear_live_ranges ();
2450 }
2451 if (fails_p)
2452 {
2453 /* It is a very rare case. It is the last hope to
2454 split a hard regno live range for a reload
2455 pseudo. */
2456 if (live_p)
2457 lra_clear_live_ranges ();
2458 live_p = false;
2459 if (! lra_split_hard_reg_for ())
2460 break;
2461 lra_hard_reg_split_p = true;
2462 }
2463 }
2464 while (fails_p);
2465 if (! live_p) {
2466 /* We need the correct reg notes for work of constraint sub-pass. */
2467 lra_create_live_ranges (true, true);
2468 live_p = true;
2469 }
2470 }
2471 /* Don't clear optional reloads bitmap until all constraints are
2472 satisfied as we need to differ them from regular reloads. */
2473 bitmap_clear (&lra_optional_reload_pseudos);
2474 bitmap_clear (&lra_subreg_reload_pseudos);
2475 bitmap_clear (&lra_inheritance_pseudos);
2476 bitmap_clear (&lra_split_regs);
2477 if (! live_p)
2478 {
2479 /* We need full live info for spilling pseudos into
2480 registers instead of memory. */
2481 lra_create_live_ranges (lra_reg_spill_p, true);
2482 live_p = true;
2483 }
2484 /* We should check necessity for spilling here as the above live
2485 range pass can remove spilled pseudos. */
2486 if (! lra_need_for_spills_p ())
2487 break;
2488 /* Now we know what pseudos should be spilled. Try to
2489 rematerialize them first. */
2490 if (lra_remat ())
2491 {
2492 /* We need full live info -- see the comment above. */
2493 lra_create_live_ranges (lra_reg_spill_p, true);
2494 live_p = true;
2495 if (! lra_need_for_spills_p ())
2496 {
2497 if (lra_need_for_scratch_reg_p ())
2498 continue;
2499 break;
2500 }
2501 }
2502 lra_spill ();
2503 /* Assignment of stack slots changes elimination offsets for
2504 some eliminations. So update the offsets here. */
2505 lra_eliminate (false, false);
2506 lra_constraint_new_regno_start = max_reg_num ();
2507 if (lra_bad_spill_regno_start == INT_MAX
2508 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2509 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2510 /* After switching off inheritance and rematerialization
2511 passes, avoid spilling reload pseudos will be created to
2512 prevent LRA cycling in some complicated cases. */
2513 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2514 lra_assignment_iter_after_spill = 0;
2515 }
2516 ira_restore_scratches (lra_dump_file);
2517 lra_eliminate (true, false);
2518 lra_final_code_change ();
2519 lra_in_progress = 0;
2520 if (live_p)
2521 lra_clear_live_ranges ();
2522 lra_live_ranges_finish ();
2523 lra_constraints_finish ();
2524 finish_reg_info ();
2525 sbitmap_free (lra_constraint_insn_stack_bitmap);
2526 lra_constraint_insn_stack.release ();
2527 finish_insn_recog_data ();
2528 regstat_free_n_sets_and_refs ();
2529 regstat_free_ri ();
2530 reload_completed = 1;
2531 update_inc_notes ();
2532
2533 inserted_p = fixup_abnormal_edges ();
2534
2535 /* We've possibly turned single trapping insn into multiple ones. */
2536 if (cfun->can_throw_non_call_exceptions)
2537 {
2538 auto_sbitmap blocks (last_basic_block_for_fn (cfun));
2539 bitmap_ones (blocks);
2540 find_many_sub_basic_blocks (blocks);
2541 }
2542
2543 if (inserted_p)
2544 commit_edge_insertions ();
2545
2546 /* Subsequent passes expect that rtl is unshared, so unshare everything
2547 here. */
2548 unshare_all_rtl_again (get_insns ());
2549
2550 if (flag_checking)
2551 check_rtl (true);
2552
2553 timevar_pop (TV_LRA);
2554 }
2555
2556 /* Called once per compiler to initialize LRA data once. */
2557 void
2558 lra_init_once (void)
2559 {
2560 init_insn_code_data_once ();
2561 }
2562
2563 /* Called once per compiler to finish LRA data which are initialize
2564 once. */
2565 void
2566 lra_finish_once (void)
2567 {
2568 finish_insn_code_data_once ();
2569 }
2570