clang-format: Enhance list of FOR_EACH macros
[official-gcc.git] / gcc / lra.c
blob0995c54aee446a49adbd67db1ec4c4beabb6788b
1 /* LRA (local register allocator) driver and LRA utilities.
2 Copyright (C) 2010-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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.
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)
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.
45 Here is block diagram of LRA passes:
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
75 -------------------------
76 | Hard regs substitution, |
77 | devirtalization, and |------> Finish
78 | restoring scratches got |
79 | memory |
80 -------------------------
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).
100 This file contains LRA driver, LRA utility functions and data, and
101 code for dealing with scratches. */
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 "tm_p.h"
113 #include "optabs.h"
114 #include "regs.h"
115 #include "ira.h"
116 #include "recog.h"
117 #include "expr.h"
118 #include "cfgrtl.h"
119 #include "cfgbuild.h"
120 #include "lra.h"
121 #include "lra-int.h"
122 #include "print-rtl.h"
124 /* Dump bitmap SET with TITLE and BB INDEX. */
125 void
126 lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
128 unsigned int i;
129 int count;
130 bitmap_iterator bi;
131 static const int max_nums_on_line = 10;
133 if (bitmap_empty_p (set))
134 return;
135 fprintf (lra_dump_file, " %s %d:", title, index);
136 fprintf (lra_dump_file, "\n");
137 count = max_nums_on_line + 1;
138 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
140 if (count > max_nums_on_line)
142 fprintf (lra_dump_file, "\n ");
143 count = 0;
145 fprintf (lra_dump_file, " %4u", i);
146 count++;
148 fprintf (lra_dump_file, "\n");
151 /* Hard registers currently not available for allocation. It can
152 changed after some hard registers become not eliminable. */
153 HARD_REG_SET lra_no_alloc_regs;
155 static int get_new_reg_value (void);
156 static void expand_reg_info (void);
157 static void invalidate_insn_recog_data (int);
158 static int get_insn_freq (rtx_insn *);
159 static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
160 rtx_insn *, int);
162 /* Expand all regno related info needed for LRA. */
163 static void
164 expand_reg_data (int old)
166 resize_reg_info ();
167 expand_reg_info ();
168 ira_expand_reg_equiv ();
169 for (int i = (int) max_reg_num () - 1; i >= old; i--)
170 lra_change_class (i, ALL_REGS, " Set", true);
173 /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
174 or of VOIDmode, use MD_MODE for the new reg. Initialize its
175 register class to RCLASS. Print message about assigning class
176 RCLASS containing new register name TITLE unless it is NULL. Use
177 attributes of ORIGINAL if it is a register. The created register
178 will have unique held value. */
180 lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
181 enum reg_class rclass, const char *title)
183 machine_mode mode;
184 rtx new_reg;
186 if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
187 mode = md_mode;
188 lra_assert (mode != VOIDmode);
189 new_reg = gen_reg_rtx (mode);
190 if (original == NULL_RTX || ! REG_P (original))
192 if (lra_dump_file != NULL)
193 fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
195 else
197 if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
198 ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
199 REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
200 REG_POINTER (new_reg) = REG_POINTER (original);
201 REG_ATTRS (new_reg) = REG_ATTRS (original);
202 if (lra_dump_file != NULL)
203 fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
204 REGNO (new_reg), REGNO (original));
206 if (lra_dump_file != NULL)
208 if (title != NULL)
209 fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
210 reg_class_names[rclass], *title == '\0' ? "" : " ",
211 title, REGNO (new_reg));
212 fprintf (lra_dump_file, "\n");
214 expand_reg_data (max_reg_num ());
215 setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
216 return new_reg;
219 /* Analogous to the previous function but also inherits value of
220 ORIGINAL. */
222 lra_create_new_reg (machine_mode md_mode, rtx original,
223 enum reg_class rclass, const char *title)
225 rtx new_reg;
227 new_reg
228 = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
229 if (original != NULL_RTX && REG_P (original))
230 lra_assign_reg_val (REGNO (original), REGNO (new_reg));
231 return new_reg;
234 /* Set up for REGNO unique hold value. */
235 void
236 lra_set_regno_unique_value (int regno)
238 lra_reg_info[regno].val = get_new_reg_value ();
241 /* Invalidate INSN related info used by LRA. The info should never be
242 used after that. */
243 void
244 lra_invalidate_insn_data (rtx_insn *insn)
246 lra_invalidate_insn_regno_info (insn);
247 invalidate_insn_recog_data (INSN_UID (insn));
250 /* Mark INSN deleted and invalidate the insn related info used by
251 LRA. */
252 void
253 lra_set_insn_deleted (rtx_insn *insn)
255 lra_invalidate_insn_data (insn);
256 SET_INSN_DELETED (insn);
259 /* Delete an unneeded INSN and any previous insns who sole purpose is
260 loading data that is dead in INSN. */
261 void
262 lra_delete_dead_insn (rtx_insn *insn)
264 rtx_insn *prev = prev_real_insn (insn);
265 rtx prev_dest;
267 /* If the previous insn sets a register that dies in our insn,
268 delete it too. */
269 if (prev && GET_CODE (PATTERN (prev)) == SET
270 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
271 && reg_mentioned_p (prev_dest, PATTERN (insn))
272 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
273 && ! side_effects_p (SET_SRC (PATTERN (prev))))
274 lra_delete_dead_insn (prev);
276 lra_set_insn_deleted (insn);
279 /* Emit insn x = y + z. Return NULL if we failed to do it.
280 Otherwise, return the insn. We don't use gen_add3_insn as it might
281 clobber CC. */
282 static rtx_insn *
283 emit_add3_insn (rtx x, rtx y, rtx z)
285 rtx_insn *last;
287 last = get_last_insn ();
289 if (have_addptr3_insn (x, y, z))
291 rtx_insn *insn = gen_addptr3_insn (x, y, z);
293 /* If the target provides an "addptr" pattern it hopefully does
294 for a reason. So falling back to the normal add would be
295 a bug. */
296 lra_assert (insn != NULL_RTX);
297 emit_insn (insn);
298 return insn;
301 rtx_insn *insn = emit_insn (gen_rtx_SET (x, gen_rtx_PLUS (GET_MODE (y),
302 y, z)));
303 if (recog_memoized (insn) < 0)
305 delete_insns_since (last);
306 insn = NULL;
308 return insn;
311 /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
312 last resort. */
313 static rtx_insn *
314 emit_add2_insn (rtx x, rtx y)
316 rtx_insn *insn = emit_add3_insn (x, x, y);
317 if (insn == NULL_RTX)
319 insn = gen_add2_insn (x, y);
320 if (insn != NULL_RTX)
321 emit_insn (insn);
323 return insn;
326 /* Target checks operands through operand predicates to recognize an
327 insn. We should have a special precaution to generate add insns
328 which are frequent results of elimination.
330 Emit insns for x = y + z. X can be used to store intermediate
331 values and should be not in Y and Z when we use X to store an
332 intermediate value. Y + Z should form [base] [+ index[ * scale]] [
333 + disp] where base and index are registers, disp and scale are
334 constants. Y should contain base if it is present, Z should
335 contain disp if any. index[*scale] can be part of Y or Z. */
336 void
337 lra_emit_add (rtx x, rtx y, rtx z)
339 int old;
340 rtx_insn *last;
341 rtx a1, a2, base, index, disp, scale, index_scale;
342 bool ok_p;
344 rtx_insn *add3_insn = emit_add3_insn (x, y, z);
345 old = max_reg_num ();
346 if (add3_insn != NULL)
348 else
350 disp = a2 = NULL_RTX;
351 if (GET_CODE (y) == PLUS)
353 a1 = XEXP (y, 0);
354 a2 = XEXP (y, 1);
355 disp = z;
357 else
359 a1 = y;
360 if (CONSTANT_P (z))
361 disp = z;
362 else
363 a2 = z;
365 index_scale = scale = NULL_RTX;
366 if (GET_CODE (a1) == MULT)
368 index_scale = a1;
369 index = XEXP (a1, 0);
370 scale = XEXP (a1, 1);
371 base = a2;
373 else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
375 index_scale = a2;
376 index = XEXP (a2, 0);
377 scale = XEXP (a2, 1);
378 base = a1;
380 else
382 base = a1;
383 index = a2;
385 if (! (REG_P (base) || GET_CODE (base) == SUBREG)
386 || (index != NULL_RTX
387 && ! (REG_P (index) || GET_CODE (index) == SUBREG))
388 || (disp != NULL_RTX && ! CONSTANT_P (disp))
389 || (scale != NULL_RTX && ! CONSTANT_P (scale)))
391 /* Probably we have no 3 op add. Last chance is to use 2-op
392 add insn. To succeed, don't move Z to X as an address
393 segment always comes in Y. Otherwise, we might fail when
394 adding the address segment to register. */
395 lra_assert (x != y && x != z);
396 emit_move_insn (x, y);
397 rtx_insn *insn = emit_add2_insn (x, z);
398 lra_assert (insn != NULL_RTX);
400 else
402 if (index_scale == NULL_RTX)
403 index_scale = index;
404 if (disp == NULL_RTX)
406 /* Generate x = index_scale; x = x + base. */
407 lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
408 emit_move_insn (x, index_scale);
409 rtx_insn *insn = emit_add2_insn (x, base);
410 lra_assert (insn != NULL_RTX);
412 else if (scale == NULL_RTX)
414 /* Try x = base + disp. */
415 lra_assert (base != NULL_RTX);
416 last = get_last_insn ();
417 rtx_insn *move_insn =
418 emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
419 if (recog_memoized (move_insn) < 0)
421 delete_insns_since (last);
422 /* Generate x = disp; x = x + base. */
423 emit_move_insn (x, disp);
424 rtx_insn *add2_insn = emit_add2_insn (x, base);
425 lra_assert (add2_insn != NULL_RTX);
427 /* Generate x = x + index. */
428 if (index != NULL_RTX)
430 rtx_insn *insn = emit_add2_insn (x, index);
431 lra_assert (insn != NULL_RTX);
434 else
436 /* Try x = index_scale; x = x + disp; x = x + base. */
437 last = get_last_insn ();
438 rtx_insn *move_insn = emit_move_insn (x, index_scale);
439 ok_p = false;
440 if (recog_memoized (move_insn) >= 0)
442 rtx_insn *insn = emit_add2_insn (x, disp);
443 if (insn != NULL_RTX)
445 insn = emit_add2_insn (x, base);
446 if (insn != NULL_RTX)
447 ok_p = true;
450 if (! ok_p)
452 delete_insns_since (last);
453 /* Generate x = disp; x = x + base; x = x + index_scale. */
454 emit_move_insn (x, disp);
455 rtx_insn *insn = emit_add2_insn (x, base);
456 lra_assert (insn != NULL_RTX);
457 insn = emit_add2_insn (x, index_scale);
458 lra_assert (insn != NULL_RTX);
463 /* Functions emit_... can create pseudos -- so expand the pseudo
464 data. */
465 if (old != max_reg_num ())
466 expand_reg_data (old);
469 /* The number of emitted reload insns so far. */
470 int lra_curr_reload_num;
472 /* Emit x := y, processing special case when y = u + v or y = u + v *
473 scale + w through emit_add (Y can be an address which is base +
474 index reg * scale + displacement in general case). X may be used
475 as intermediate result therefore it should be not in Y. */
476 void
477 lra_emit_move (rtx x, rtx y)
479 int old;
481 if (GET_CODE (y) != PLUS)
483 if (rtx_equal_p (x, y))
484 return;
485 old = max_reg_num ();
486 emit_move_insn (x, y);
487 if (REG_P (x))
488 lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
489 /* Function emit_move can create pseudos -- so expand the pseudo
490 data. */
491 if (old != max_reg_num ())
492 expand_reg_data (old);
493 return;
495 lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
498 /* Update insn operands which are duplication of operands whose
499 numbers are in array of NOPS (with end marker -1). The insn is
500 represented by its LRA internal representation ID. */
501 void
502 lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
504 int i, j, nop;
505 struct lra_static_insn_data *static_id = id->insn_static_data;
507 for (i = 0; i < static_id->n_dups; i++)
508 for (j = 0; (nop = nops[j]) >= 0; j++)
509 if (static_id->dup_num[i] == nop)
510 *id->dup_loc[i] = *id->operand_loc[nop];
515 /* This page contains code dealing with info about registers in the
516 insns. */
518 /* Pools for insn reg info. */
519 object_allocator<lra_insn_reg> lra_insn_reg_pool ("insn regs");
521 /* Create LRA insn related info about a reference to REGNO in INSN with
522 TYPE (in/out/inout), biggest reference mode MODE, flag that it is
523 reference through subreg (SUBREG_P), flag that is early clobbered
524 in the insn (EARLY_CLOBBER), and reference to the next insn reg
525 info (NEXT). */
526 static struct lra_insn_reg *
527 new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
528 machine_mode mode,
529 bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
531 lra_insn_reg *ir = lra_insn_reg_pool.allocate ();
532 ir->type = type;
533 ir->biggest_mode = mode;
534 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
535 && NONDEBUG_INSN_P (insn))
536 lra_reg_info[regno].biggest_mode = mode;
537 ir->subreg_p = subreg_p;
538 ir->early_clobber = early_clobber;
539 ir->regno = regno;
540 ir->next = next;
541 return ir;
544 /* Free insn reg info list IR. */
545 static void
546 free_insn_regs (struct lra_insn_reg *ir)
548 struct lra_insn_reg *next_ir;
550 for (; ir != NULL; ir = next_ir)
552 next_ir = ir->next;
553 lra_insn_reg_pool.remove (ir);
557 /* Finish pool for insn reg info. */
558 static void
559 finish_insn_regs (void)
561 lra_insn_reg_pool.release ();
566 /* This page contains code dealing LRA insn info (or in other words
567 LRA internal insn representation). */
569 /* Map INSN_CODE -> the static insn data. This info is valid during
570 all translation unit. */
571 struct lra_static_insn_data *insn_code_data[NUM_INSN_CODES];
573 /* Debug insns are represented as a special insn with one input
574 operand which is RTL expression in var_location. */
576 /* The following data are used as static insn operand data for all
577 debug insns. If structure lra_operand_data is changed, the
578 initializer should be changed too. */
579 static struct lra_operand_data debug_operand_data =
581 NULL, /* alternative */
582 VOIDmode, /* We are not interesting in the operand mode. */
583 OP_IN,
584 0, 0, 0, 0
587 /* The following data are used as static insn data for all debug
588 insns. If structure lra_static_insn_data is changed, the
589 initializer should be changed too. */
590 static struct lra_static_insn_data debug_insn_static_data =
592 &debug_operand_data,
593 0, /* Duplication operands #. */
594 -1, /* Commutative operand #. */
595 1, /* Operands #. There is only one operand which is debug RTL
596 expression. */
597 0, /* Duplications #. */
598 0, /* Alternatives #. We are not interesting in alternatives
599 because we does not proceed debug_insns for reloads. */
600 NULL, /* Hard registers referenced in machine description. */
601 NULL /* Descriptions of operands in alternatives. */
604 /* Called once per compiler work to initialize some LRA data related
605 to insns. */
606 static void
607 init_insn_code_data_once (void)
609 memset (insn_code_data, 0, sizeof (insn_code_data));
612 /* Called once per compiler work to finalize some LRA data related to
613 insns. */
614 static void
615 finish_insn_code_data_once (void)
617 for (unsigned int i = 0; i < NUM_INSN_CODES; i++)
619 if (insn_code_data[i] != NULL)
620 free (insn_code_data[i]);
624 /* Return static insn data, allocate and setup if necessary. Although
625 dup_num is static data (it depends only on icode), to set it up we
626 need to extract insn first. So recog_data should be valid for
627 normal insn (ICODE >= 0) before the call. */
628 static struct lra_static_insn_data *
629 get_static_insn_data (int icode, int nop, int ndup, int nalt)
631 struct lra_static_insn_data *data;
632 size_t n_bytes;
634 lra_assert (icode < (int) NUM_INSN_CODES);
635 if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
636 return data;
637 lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
638 n_bytes = sizeof (struct lra_static_insn_data)
639 + sizeof (struct lra_operand_data) * nop
640 + sizeof (int) * ndup;
641 data = XNEWVAR (struct lra_static_insn_data, n_bytes);
642 data->operand_alternative = NULL;
643 data->n_operands = nop;
644 data->n_dups = ndup;
645 data->n_alternatives = nalt;
646 data->operand = ((struct lra_operand_data *)
647 ((char *) data + sizeof (struct lra_static_insn_data)));
648 data->dup_num = ((int *) ((char *) data->operand
649 + sizeof (struct lra_operand_data) * nop));
650 if (icode >= 0)
652 int i;
654 insn_code_data[icode] = data;
655 for (i = 0; i < nop; i++)
657 data->operand[i].constraint
658 = insn_data[icode].operand[i].constraint;
659 data->operand[i].mode = insn_data[icode].operand[i].mode;
660 data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
661 data->operand[i].is_operator
662 = insn_data[icode].operand[i].is_operator;
663 data->operand[i].type
664 = (data->operand[i].constraint[0] == '=' ? OP_OUT
665 : data->operand[i].constraint[0] == '+' ? OP_INOUT
666 : OP_IN);
667 data->operand[i].is_address = false;
669 for (i = 0; i < ndup; i++)
670 data->dup_num[i] = recog_data.dup_num[i];
672 return data;
675 /* The current length of the following array. */
676 int lra_insn_recog_data_len;
678 /* Map INSN_UID -> the insn recog data (NULL if unknown). */
679 lra_insn_recog_data_t *lra_insn_recog_data;
681 /* Initialize LRA data about insns. */
682 static void
683 init_insn_recog_data (void)
685 lra_insn_recog_data_len = 0;
686 lra_insn_recog_data = NULL;
689 /* Expand, if necessary, LRA data about insns. */
690 static void
691 check_and_expand_insn_recog_data (int index)
693 int i, old;
695 if (lra_insn_recog_data_len > index)
696 return;
697 old = lra_insn_recog_data_len;
698 lra_insn_recog_data_len = index * 3 / 2 + 1;
699 lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
700 lra_insn_recog_data,
701 lra_insn_recog_data_len);
702 for (i = old; i < lra_insn_recog_data_len; i++)
703 lra_insn_recog_data[i] = NULL;
706 /* Finish LRA DATA about insn. */
707 static void
708 free_insn_recog_data (lra_insn_recog_data_t data)
710 if (data->operand_loc != NULL)
711 free (data->operand_loc);
712 if (data->dup_loc != NULL)
713 free (data->dup_loc);
714 if (data->arg_hard_regs != NULL)
715 free (data->arg_hard_regs);
716 if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
718 if (data->insn_static_data->operand_alternative != NULL)
719 free (const_cast <operand_alternative *>
720 (data->insn_static_data->operand_alternative));
721 free_insn_regs (data->insn_static_data->hard_regs);
722 free (data->insn_static_data);
724 free_insn_regs (data->regs);
725 data->regs = NULL;
726 free (data);
729 /* Pools for copies. */
730 static object_allocator<lra_copy> lra_copy_pool ("lra copies");
732 /* Finish LRA data about all insns. */
733 static void
734 finish_insn_recog_data (void)
736 int i;
737 lra_insn_recog_data_t data;
739 for (i = 0; i < lra_insn_recog_data_len; i++)
740 if ((data = lra_insn_recog_data[i]) != NULL)
741 free_insn_recog_data (data);
742 finish_insn_regs ();
743 lra_copy_pool.release ();
744 lra_insn_reg_pool.release ();
745 free (lra_insn_recog_data);
748 /* Setup info about operands in alternatives of LRA DATA of insn. */
749 static void
750 setup_operand_alternative (lra_insn_recog_data_t data,
751 const operand_alternative *op_alt)
753 int i, j, nop, nalt;
754 int icode = data->icode;
755 struct lra_static_insn_data *static_data = data->insn_static_data;
757 static_data->commutative = -1;
758 nop = static_data->n_operands;
759 nalt = static_data->n_alternatives;
760 static_data->operand_alternative = op_alt;
761 for (i = 0; i < nop; i++)
763 static_data->operand[i].early_clobber = false;
764 static_data->operand[i].is_address = false;
765 if (static_data->operand[i].constraint[0] == '%')
767 /* We currently only support one commutative pair of operands. */
768 if (static_data->commutative < 0)
769 static_data->commutative = i;
770 else
771 lra_assert (icode < 0); /* Asm */
772 /* The last operand should not be marked commutative. */
773 lra_assert (i != nop - 1);
776 for (j = 0; j < nalt; j++)
777 for (i = 0; i < nop; i++, op_alt++)
779 static_data->operand[i].early_clobber |= op_alt->earlyclobber;
780 static_data->operand[i].is_address |= op_alt->is_address;
784 /* Recursively process X and collect info about registers, which are
785 not the insn operands, in X with TYPE (in/out/inout) and flag that
786 it is early clobbered in the insn (EARLY_CLOBBER) and add the info
787 to LIST. X is a part of insn given by DATA. Return the result
788 list. */
789 static struct lra_insn_reg *
790 collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
791 struct lra_insn_reg *list,
792 enum op_type type, bool early_clobber)
794 int i, j, regno, last;
795 bool subreg_p;
796 machine_mode mode;
797 struct lra_insn_reg *curr;
798 rtx op = *x;
799 enum rtx_code code = GET_CODE (op);
800 const char *fmt = GET_RTX_FORMAT (code);
802 for (i = 0; i < data->insn_static_data->n_operands; i++)
803 if (x == data->operand_loc[i])
804 /* It is an operand loc. Stop here. */
805 return list;
806 for (i = 0; i < data->insn_static_data->n_dups; i++)
807 if (x == data->dup_loc[i])
808 /* It is a dup loc. Stop here. */
809 return list;
810 mode = GET_MODE (op);
811 subreg_p = false;
812 if (code == SUBREG)
814 op = SUBREG_REG (op);
815 code = GET_CODE (op);
816 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
818 mode = GET_MODE (op);
819 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
820 subreg_p = true;
823 if (REG_P (op))
825 if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
826 return list;
827 /* Process all regs even unallocatable ones as we need info
828 about all regs for rematerialization pass. */
829 for (last = regno + hard_regno_nregs[regno][mode];
830 regno < last;
831 regno++)
833 for (curr = list; curr != NULL; curr = curr->next)
834 if (curr->regno == regno && curr->subreg_p == subreg_p
835 && curr->biggest_mode == mode)
837 if (curr->type != type)
838 curr->type = OP_INOUT;
839 if (curr->early_clobber != early_clobber)
840 curr->early_clobber = true;
841 break;
843 if (curr == NULL)
845 /* This is a new hard regno or the info can not be
846 integrated into the found structure. */
847 #ifdef STACK_REGS
848 early_clobber
849 = (early_clobber
850 /* This clobber is to inform popping floating
851 point stack only. */
852 && ! (FIRST_STACK_REG <= regno
853 && regno <= LAST_STACK_REG));
854 #endif
855 list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
856 early_clobber, list);
859 return list;
861 switch (code)
863 case SET:
864 list = collect_non_operand_hard_regs (&SET_DEST (op), data,
865 list, OP_OUT, false);
866 list = collect_non_operand_hard_regs (&SET_SRC (op), data,
867 list, OP_IN, false);
868 break;
869 case CLOBBER:
870 /* We treat clobber of non-operand hard registers as early
871 clobber (the behavior is expected from asm). */
872 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
873 list, OP_OUT, true);
874 break;
875 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
876 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
877 list, OP_INOUT, false);
878 break;
879 case PRE_MODIFY: case POST_MODIFY:
880 list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
881 list, OP_INOUT, false);
882 list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
883 list, OP_IN, false);
884 break;
885 default:
886 fmt = GET_RTX_FORMAT (code);
887 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
889 if (fmt[i] == 'e')
890 list = collect_non_operand_hard_regs (&XEXP (op, i), data,
891 list, OP_IN, false);
892 else if (fmt[i] == 'E')
893 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
894 list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
895 list, OP_IN, false);
898 return list;
901 /* Set up and return info about INSN. Set up the info if it is not set up
902 yet. */
903 lra_insn_recog_data_t
904 lra_set_insn_recog_data (rtx_insn *insn)
906 lra_insn_recog_data_t data;
907 int i, n, icode;
908 rtx **locs;
909 unsigned int uid = INSN_UID (insn);
910 struct lra_static_insn_data *insn_static_data;
912 check_and_expand_insn_recog_data (uid);
913 if (DEBUG_INSN_P (insn))
914 icode = -1;
915 else
917 icode = INSN_CODE (insn);
918 if (icode < 0)
919 /* It might be a new simple insn which is not recognized yet. */
920 INSN_CODE (insn) = icode = recog_memoized (insn);
922 data = XNEW (struct lra_insn_recog_data);
923 lra_insn_recog_data[uid] = data;
924 data->insn = insn;
925 data->used_insn_alternative = -1;
926 data->icode = icode;
927 data->regs = NULL;
928 if (DEBUG_INSN_P (insn))
930 data->insn_static_data = &debug_insn_static_data;
931 data->dup_loc = NULL;
932 data->arg_hard_regs = NULL;
933 data->preferred_alternatives = ALL_ALTERNATIVES;
934 data->operand_loc = XNEWVEC (rtx *, 1);
935 data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
936 return data;
938 if (icode < 0)
940 int nop, nalt;
941 machine_mode operand_mode[MAX_RECOG_OPERANDS];
942 const char *constraints[MAX_RECOG_OPERANDS];
944 nop = asm_noperands (PATTERN (insn));
945 data->operand_loc = data->dup_loc = NULL;
946 nalt = 1;
947 if (nop < 0)
949 /* It is a special insn like USE or CLOBBER. We should
950 recognize any regular insn otherwise LRA can do nothing
951 with this insn. */
952 gcc_assert (GET_CODE (PATTERN (insn)) == USE
953 || GET_CODE (PATTERN (insn)) == CLOBBER
954 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
955 data->insn_static_data = insn_static_data
956 = get_static_insn_data (-1, 0, 0, nalt);
958 else
960 /* expand_asm_operands makes sure there aren't too many
961 operands. */
962 lra_assert (nop <= MAX_RECOG_OPERANDS);
963 if (nop != 0)
964 data->operand_loc = XNEWVEC (rtx *, nop);
965 /* Now get the operand values and constraints out of the
966 insn. */
967 decode_asm_operands (PATTERN (insn), NULL,
968 data->operand_loc,
969 constraints, operand_mode, NULL);
970 if (nop > 0)
972 const char *p = recog_data.constraints[0];
974 for (p = constraints[0]; *p; p++)
975 nalt += *p == ',';
977 data->insn_static_data = insn_static_data
978 = get_static_insn_data (-1, nop, 0, nalt);
979 for (i = 0; i < nop; i++)
981 insn_static_data->operand[i].mode = operand_mode[i];
982 insn_static_data->operand[i].constraint = constraints[i];
983 insn_static_data->operand[i].strict_low = false;
984 insn_static_data->operand[i].is_operator = false;
985 insn_static_data->operand[i].is_address = false;
988 for (i = 0; i < insn_static_data->n_operands; i++)
989 insn_static_data->operand[i].type
990 = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
991 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
992 : OP_IN);
993 data->preferred_alternatives = ALL_ALTERNATIVES;
994 if (nop > 0)
996 operand_alternative *op_alt = XCNEWVEC (operand_alternative,
997 nalt * nop);
998 preprocess_constraints (nop, nalt, constraints, op_alt);
999 setup_operand_alternative (data, op_alt);
1002 else
1004 insn_extract (insn);
1005 data->insn_static_data = insn_static_data
1006 = get_static_insn_data (icode, insn_data[icode].n_operands,
1007 insn_data[icode].n_dups,
1008 insn_data[icode].n_alternatives);
1009 n = insn_static_data->n_operands;
1010 if (n == 0)
1011 locs = NULL;
1012 else
1014 locs = XNEWVEC (rtx *, n);
1015 memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
1017 data->operand_loc = locs;
1018 n = insn_static_data->n_dups;
1019 if (n == 0)
1020 locs = NULL;
1021 else
1023 locs = XNEWVEC (rtx *, n);
1024 memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
1026 data->dup_loc = locs;
1027 data->preferred_alternatives = get_preferred_alternatives (insn);
1028 const operand_alternative *op_alt = preprocess_insn_constraints (icode);
1029 if (!insn_static_data->operand_alternative)
1030 setup_operand_alternative (data, op_alt);
1031 else if (op_alt != insn_static_data->operand_alternative)
1032 insn_static_data->operand_alternative = op_alt;
1034 if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
1035 insn_static_data->hard_regs = NULL;
1036 else
1037 insn_static_data->hard_regs
1038 = collect_non_operand_hard_regs (&PATTERN (insn), data,
1039 NULL, OP_IN, false);
1040 data->arg_hard_regs = NULL;
1041 if (CALL_P (insn))
1043 bool use_p;
1044 rtx link;
1045 int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
1047 n_hard_regs = 0;
1048 /* Finding implicit hard register usage. We believe it will be
1049 not changed whatever transformations are used. Call insns
1050 are such example. */
1051 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1052 link != NULL_RTX;
1053 link = XEXP (link, 1))
1054 if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
1055 || GET_CODE (XEXP (link, 0)) == CLOBBER)
1056 && REG_P (XEXP (XEXP (link, 0), 0)))
1058 regno = REGNO (XEXP (XEXP (link, 0), 0));
1059 lra_assert (regno < FIRST_PSEUDO_REGISTER);
1060 /* It is an argument register. */
1061 for (i = REG_NREGS (XEXP (XEXP (link, 0), 0)) - 1; i >= 0; i--)
1062 arg_hard_regs[n_hard_regs++]
1063 = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
1065 if (n_hard_regs != 0)
1067 arg_hard_regs[n_hard_regs++] = -1;
1068 data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
1069 memcpy (data->arg_hard_regs, arg_hard_regs,
1070 sizeof (int) * n_hard_regs);
1073 /* Some output operand can be recognized only from the context not
1074 from the constraints which are empty in this case. Call insn may
1075 contain a hard register in set destination with empty constraint
1076 and extract_insn treats them as an input. */
1077 for (i = 0; i < insn_static_data->n_operands; i++)
1079 int j;
1080 rtx pat, set;
1081 struct lra_operand_data *operand = &insn_static_data->operand[i];
1083 /* ??? Should we treat 'X' the same way. It looks to me that
1084 'X' means anything and empty constraint means we do not
1085 care. */
1086 if (operand->type != OP_IN || *operand->constraint != '\0'
1087 || operand->is_operator)
1088 continue;
1089 pat = PATTERN (insn);
1090 if (GET_CODE (pat) == SET)
1092 if (data->operand_loc[i] != &SET_DEST (pat))
1093 continue;
1095 else if (GET_CODE (pat) == PARALLEL)
1097 for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
1099 set = XVECEXP (PATTERN (insn), 0, j);
1100 if (GET_CODE (set) == SET
1101 && &SET_DEST (set) == data->operand_loc[i])
1102 break;
1104 if (j < 0)
1105 continue;
1107 else
1108 continue;
1109 operand->type = OP_OUT;
1111 return data;
1114 /* Return info about insn give by UID. The info should be already set
1115 up. */
1116 static lra_insn_recog_data_t
1117 get_insn_recog_data_by_uid (int uid)
1119 lra_insn_recog_data_t data;
1121 data = lra_insn_recog_data[uid];
1122 lra_assert (data != NULL);
1123 return data;
1126 /* Invalidate all info about insn given by its UID. */
1127 static void
1128 invalidate_insn_recog_data (int uid)
1130 lra_insn_recog_data_t data;
1132 data = lra_insn_recog_data[uid];
1133 lra_assert (data != NULL);
1134 free_insn_recog_data (data);
1135 lra_insn_recog_data[uid] = NULL;
1138 /* Update all the insn info about INSN. It is usually called when
1139 something in the insn was changed. Return the updated info. */
1140 lra_insn_recog_data_t
1141 lra_update_insn_recog_data (rtx_insn *insn)
1143 lra_insn_recog_data_t data;
1144 int n;
1145 unsigned int uid = INSN_UID (insn);
1146 struct lra_static_insn_data *insn_static_data;
1147 HOST_WIDE_INT sp_offset = 0;
1149 check_and_expand_insn_recog_data (uid);
1150 if ((data = lra_insn_recog_data[uid]) != NULL
1151 && data->icode != INSN_CODE (insn))
1153 sp_offset = data->sp_offset;
1154 invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
1155 invalidate_insn_recog_data (uid);
1156 data = NULL;
1158 if (data == NULL)
1160 data = lra_get_insn_recog_data (insn);
1161 /* Initiate or restore SP offset. */
1162 data->sp_offset = sp_offset;
1163 return data;
1165 insn_static_data = data->insn_static_data;
1166 data->used_insn_alternative = -1;
1167 if (DEBUG_INSN_P (insn))
1168 return data;
1169 if (data->icode < 0)
1171 int nop;
1172 machine_mode operand_mode[MAX_RECOG_OPERANDS];
1173 const char *constraints[MAX_RECOG_OPERANDS];
1175 nop = asm_noperands (PATTERN (insn));
1176 if (nop >= 0)
1178 lra_assert (nop == data->insn_static_data->n_operands);
1179 /* Now get the operand values and constraints out of the
1180 insn. */
1181 decode_asm_operands (PATTERN (insn), NULL,
1182 data->operand_loc,
1183 constraints, operand_mode, NULL);
1185 if (flag_checking)
1186 for (int i = 0; i < nop; i++)
1187 lra_assert
1188 (insn_static_data->operand[i].mode == operand_mode[i]
1189 && insn_static_data->operand[i].constraint == constraints[i]
1190 && ! insn_static_data->operand[i].is_operator);
1193 if (flag_checking)
1194 for (int i = 0; i < insn_static_data->n_operands; i++)
1195 lra_assert
1196 (insn_static_data->operand[i].type
1197 == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
1198 : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
1199 : OP_IN));
1201 else
1203 insn_extract (insn);
1204 n = insn_static_data->n_operands;
1205 if (n != 0)
1206 memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
1207 n = insn_static_data->n_dups;
1208 if (n != 0)
1209 memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
1210 lra_assert (check_bool_attrs (insn));
1212 return data;
1215 /* Set up that INSN is using alternative ALT now. */
1216 void
1217 lra_set_used_insn_alternative (rtx_insn *insn, int alt)
1219 lra_insn_recog_data_t data;
1221 data = lra_get_insn_recog_data (insn);
1222 data->used_insn_alternative = alt;
1225 /* Set up that insn with UID is using alternative ALT now. The insn
1226 info should be already set up. */
1227 void
1228 lra_set_used_insn_alternative_by_uid (int uid, int alt)
1230 lra_insn_recog_data_t data;
1232 check_and_expand_insn_recog_data (uid);
1233 data = lra_insn_recog_data[uid];
1234 lra_assert (data != NULL);
1235 data->used_insn_alternative = alt;
1240 /* This page contains code dealing with common register info and
1241 pseudo copies. */
1243 /* The size of the following array. */
1244 static int reg_info_size;
1245 /* Common info about each register. */
1246 struct lra_reg *lra_reg_info;
1248 /* Last register value. */
1249 static int last_reg_value;
1251 /* Return new register value. */
1252 static int
1253 get_new_reg_value (void)
1255 return ++last_reg_value;
1258 /* Vec referring to pseudo copies. */
1259 static vec<lra_copy_t> copy_vec;
1261 /* Initialize I-th element of lra_reg_info. */
1262 static inline void
1263 initialize_lra_reg_info_element (int i)
1265 bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
1266 #ifdef STACK_REGS
1267 lra_reg_info[i].no_stack_p = false;
1268 #endif
1269 CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
1270 CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
1271 lra_reg_info[i].preferred_hard_regno1 = -1;
1272 lra_reg_info[i].preferred_hard_regno2 = -1;
1273 lra_reg_info[i].preferred_hard_regno_profit1 = 0;
1274 lra_reg_info[i].preferred_hard_regno_profit2 = 0;
1275 lra_reg_info[i].biggest_mode = VOIDmode;
1276 lra_reg_info[i].live_ranges = NULL;
1277 lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
1278 lra_reg_info[i].last_reload = 0;
1279 lra_reg_info[i].restore_regno = -1;
1280 lra_reg_info[i].val = get_new_reg_value ();
1281 lra_reg_info[i].offset = 0;
1282 lra_reg_info[i].copies = NULL;
1285 /* Initialize common reg info and copies. */
1286 static void
1287 init_reg_info (void)
1289 int i;
1291 last_reg_value = 0;
1292 reg_info_size = max_reg_num () * 3 / 2 + 1;
1293 lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
1294 for (i = 0; i < reg_info_size; i++)
1295 initialize_lra_reg_info_element (i);
1296 copy_vec.truncate (0);
1300 /* Finish common reg info and copies. */
1301 static void
1302 finish_reg_info (void)
1304 int i;
1306 for (i = 0; i < reg_info_size; i++)
1307 bitmap_clear (&lra_reg_info[i].insn_bitmap);
1308 free (lra_reg_info);
1309 reg_info_size = 0;
1312 /* Expand common reg info if it is necessary. */
1313 static void
1314 expand_reg_info (void)
1316 int i, old = reg_info_size;
1318 if (reg_info_size > max_reg_num ())
1319 return;
1320 reg_info_size = max_reg_num () * 3 / 2 + 1;
1321 lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
1322 for (i = old; i < reg_info_size; i++)
1323 initialize_lra_reg_info_element (i);
1326 /* Free all copies. */
1327 void
1328 lra_free_copies (void)
1330 lra_copy_t cp;
1332 while (copy_vec.length () != 0)
1334 cp = copy_vec.pop ();
1335 lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
1336 lra_copy_pool.remove (cp);
1340 /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
1341 frequency is FREQ. */
1342 void
1343 lra_create_copy (int regno1, int regno2, int freq)
1345 bool regno1_dest_p;
1346 lra_copy_t cp;
1348 lra_assert (regno1 != regno2);
1349 regno1_dest_p = true;
1350 if (regno1 > regno2)
1352 std::swap (regno1, regno2);
1353 regno1_dest_p = false;
1355 cp = lra_copy_pool.allocate ();
1356 copy_vec.safe_push (cp);
1357 cp->regno1_dest_p = regno1_dest_p;
1358 cp->freq = freq;
1359 cp->regno1 = regno1;
1360 cp->regno2 = regno2;
1361 cp->regno1_next = lra_reg_info[regno1].copies;
1362 lra_reg_info[regno1].copies = cp;
1363 cp->regno2_next = lra_reg_info[regno2].copies;
1364 lra_reg_info[regno2].copies = cp;
1365 if (lra_dump_file != NULL)
1366 fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
1367 regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
1370 /* Return N-th (0, 1, ...) copy. If there is no copy, return
1371 NULL. */
1372 lra_copy_t
1373 lra_get_copy (int n)
1375 if (n >= (int) copy_vec.length ())
1376 return NULL;
1377 return copy_vec[n];
1382 /* This page contains code dealing with info about registers in
1383 insns. */
1385 /* Process X of insn UID recursively and add info (operand type is
1386 given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
1387 about registers in X to the insn DATA. */
1388 static void
1389 add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
1390 enum op_type type, bool early_clobber)
1392 int i, j, regno;
1393 bool subreg_p;
1394 machine_mode mode;
1395 const char *fmt;
1396 enum rtx_code code;
1397 struct lra_insn_reg *curr;
1399 code = GET_CODE (x);
1400 mode = GET_MODE (x);
1401 subreg_p = false;
1402 if (GET_CODE (x) == SUBREG)
1404 x = SUBREG_REG (x);
1405 code = GET_CODE (x);
1406 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
1408 mode = GET_MODE (x);
1409 if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
1410 subreg_p = true;
1413 if (REG_P (x))
1415 regno = REGNO (x);
1416 /* Process all regs even unallocatable ones as we need info about
1417 all regs for rematerialization pass. */
1418 expand_reg_info ();
1419 if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
1421 data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
1422 early_clobber, data->regs);
1423 return;
1425 else
1427 for (curr = data->regs; curr != NULL; curr = curr->next)
1428 if (curr->regno == regno)
1430 if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
1431 /* The info can not be integrated into the found
1432 structure. */
1433 data->regs = new_insn_reg (data->insn, regno, type, mode,
1434 subreg_p, early_clobber,
1435 data->regs);
1436 else
1438 if (curr->type != type)
1439 curr->type = OP_INOUT;
1440 if (curr->early_clobber != early_clobber)
1441 curr->early_clobber = true;
1443 return;
1445 gcc_unreachable ();
1449 switch (code)
1451 case SET:
1452 add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
1453 add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
1454 break;
1455 case CLOBBER:
1456 /* We treat clobber of non-operand hard registers as early
1457 clobber (the behavior is expected from asm). */
1458 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
1459 break;
1460 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
1461 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1462 break;
1463 case PRE_MODIFY: case POST_MODIFY:
1464 add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
1465 add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
1466 break;
1467 default:
1468 if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
1469 /* Some targets place small structures in registers for return
1470 values of functions, and those registers are wrapped in
1471 PARALLEL that we may see as the destination of a SET. Here
1472 is an example:
1474 (call_insn 13 12 14 2 (set (parallel:BLK [
1475 (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
1476 (const_int 0 [0]))
1477 (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
1478 (const_int 8 [0x8]))
1480 (call (mem:QI (symbol_ref:DI (... */
1481 type = OP_IN;
1482 fmt = GET_RTX_FORMAT (code);
1483 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1485 if (fmt[i] == 'e')
1486 add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
1487 else if (fmt[i] == 'E')
1489 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1490 add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
1491 type, false);
1497 /* Return execution frequency of INSN. */
1498 static int
1499 get_insn_freq (rtx_insn *insn)
1501 basic_block bb = BLOCK_FOR_INSN (insn);
1503 gcc_checking_assert (bb != NULL);
1504 return REG_FREQ_FROM_BB (bb);
1507 /* Invalidate all reg info of INSN with DATA and execution frequency
1508 FREQ. Update common info about the invalidated registers. */
1509 static void
1510 invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
1511 int freq)
1513 int uid;
1514 bool debug_p;
1515 unsigned int i;
1516 struct lra_insn_reg *ir, *next_ir;
1518 uid = INSN_UID (insn);
1519 debug_p = DEBUG_INSN_P (insn);
1520 for (ir = data->regs; ir != NULL; ir = next_ir)
1522 i = ir->regno;
1523 next_ir = ir->next;
1524 lra_insn_reg_pool.remove (ir);
1525 bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
1526 if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
1528 lra_reg_info[i].nrefs--;
1529 lra_reg_info[i].freq -= freq;
1530 lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
1533 data->regs = NULL;
1536 /* Invalidate all reg info of INSN. Update common info about the
1537 invalidated registers. */
1538 void
1539 lra_invalidate_insn_regno_info (rtx_insn *insn)
1541 invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
1542 get_insn_freq (insn));
1545 /* Update common reg info from reg info of insn given by its DATA and
1546 execution frequency FREQ. */
1547 static void
1548 setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
1550 unsigned int i;
1551 struct lra_insn_reg *ir;
1553 for (ir = data->regs; ir != NULL; ir = ir->next)
1554 if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
1556 lra_reg_info[i].nrefs++;
1557 lra_reg_info[i].freq += freq;
1561 /* Set up insn reg info of INSN. Update common reg info from reg info
1562 of INSN. */
1563 void
1564 lra_update_insn_regno_info (rtx_insn *insn)
1566 int i, uid, freq;
1567 lra_insn_recog_data_t data;
1568 struct lra_static_insn_data *static_data;
1569 enum rtx_code code;
1570 rtx link;
1572 if (! INSN_P (insn))
1573 return;
1574 data = lra_get_insn_recog_data (insn);
1575 static_data = data->insn_static_data;
1576 freq = get_insn_freq (insn);
1577 invalidate_insn_data_regno_info (data, insn, freq);
1578 uid = INSN_UID (insn);
1579 for (i = static_data->n_operands - 1; i >= 0; i--)
1580 add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
1581 static_data->operand[i].type,
1582 static_data->operand[i].early_clobber);
1583 if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
1584 add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
1585 code == USE ? OP_IN : OP_OUT, false);
1586 if (CALL_P (insn))
1587 /* On some targets call insns can refer to pseudos in memory in
1588 CALL_INSN_FUNCTION_USAGE list. Process them in order to
1589 consider their occurrences in calls for different
1590 transformations (e.g. inheritance) with given pseudos. */
1591 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1592 link != NULL_RTX;
1593 link = XEXP (link, 1))
1594 if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
1595 && MEM_P (XEXP (XEXP (link, 0), 0)))
1596 add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
1597 code == USE ? OP_IN : OP_OUT, false);
1598 if (NONDEBUG_INSN_P (insn))
1599 setup_insn_reg_info (data, freq);
1602 /* Return reg info of insn given by it UID. */
1603 struct lra_insn_reg *
1604 lra_get_insn_regs (int uid)
1606 lra_insn_recog_data_t data;
1608 data = get_insn_recog_data_by_uid (uid);
1609 return data->regs;
1614 /* This page contains code dealing with stack of the insns which
1615 should be processed by the next constraint pass. */
1617 /* Bitmap used to put an insn on the stack only in one exemplar. */
1618 static sbitmap lra_constraint_insn_stack_bitmap;
1620 /* The stack itself. */
1621 vec<rtx_insn *> lra_constraint_insn_stack;
1623 /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
1624 info for INSN, otherwise only update it if INSN is not already on the
1625 stack. */
1626 static inline void
1627 lra_push_insn_1 (rtx_insn *insn, bool always_update)
1629 unsigned int uid = INSN_UID (insn);
1630 if (always_update)
1631 lra_update_insn_regno_info (insn);
1632 if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
1633 lra_constraint_insn_stack_bitmap =
1634 sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
1635 if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
1636 return;
1637 bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
1638 if (! always_update)
1639 lra_update_insn_regno_info (insn);
1640 lra_constraint_insn_stack.safe_push (insn);
1643 /* Put INSN on the stack. */
1644 void
1645 lra_push_insn (rtx_insn *insn)
1647 lra_push_insn_1 (insn, false);
1650 /* Put INSN on the stack and update its reg info. */
1651 void
1652 lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
1654 lra_push_insn_1 (insn, true);
1657 /* Put insn with UID on the stack. */
1658 void
1659 lra_push_insn_by_uid (unsigned int uid)
1661 lra_push_insn (lra_insn_recog_data[uid]->insn);
1664 /* Take the last-inserted insns off the stack and return it. */
1665 rtx_insn *
1666 lra_pop_insn (void)
1668 rtx_insn *insn = lra_constraint_insn_stack.pop ();
1669 bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
1670 return insn;
1673 /* Return the current size of the insn stack. */
1674 unsigned int
1675 lra_insn_stack_length (void)
1677 return lra_constraint_insn_stack.length ();
1680 /* Push insns FROM to TO (excluding it) going in reverse order. */
1681 static void
1682 push_insns (rtx_insn *from, rtx_insn *to)
1684 rtx_insn *insn;
1686 if (from == NULL_RTX)
1687 return;
1688 for (insn = from; insn != to; insn = PREV_INSN (insn))
1689 if (INSN_P (insn))
1690 lra_push_insn (insn);
1693 /* Set up sp offset for insn in range [FROM, LAST]. The offset is
1694 taken from the next BB insn after LAST or zero if there in such
1695 insn. */
1696 static void
1697 setup_sp_offset (rtx_insn *from, rtx_insn *last)
1699 rtx_insn *before = next_nonnote_insn_bb (last);
1700 HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
1701 ? 0 : lra_get_insn_recog_data (before)->sp_offset);
1703 for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
1704 lra_get_insn_recog_data (insn)->sp_offset = offset;
1707 /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
1708 insns onto the stack. Print about emitting the insns with
1709 TITLE. */
1710 void
1711 lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
1712 const char *title)
1714 rtx_insn *last;
1716 if (before == NULL_RTX && after == NULL_RTX)
1717 return;
1718 if (lra_dump_file != NULL)
1720 dump_insn_slim (lra_dump_file, insn);
1721 if (before != NULL_RTX)
1723 fprintf (lra_dump_file," %s before:\n", title);
1724 dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
1726 if (after != NULL_RTX)
1728 fprintf (lra_dump_file, " %s after:\n", title);
1729 dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
1731 fprintf (lra_dump_file, "\n");
1733 if (before != NULL_RTX)
1735 emit_insn_before (before, insn);
1736 push_insns (PREV_INSN (insn), PREV_INSN (before));
1737 setup_sp_offset (before, PREV_INSN (insn));
1739 if (after != NULL_RTX)
1741 for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
1743 emit_insn_after (after, insn);
1744 push_insns (last, insn);
1745 setup_sp_offset (after, last);
1751 /* Replace all references to register OLD_REGNO in *LOC with pseudo
1752 register NEW_REG. Try to simplify subreg of constant if SUBREG_P.
1753 Return true if any change was made. */
1754 bool
1755 lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg, bool subreg_p)
1757 rtx x = *loc;
1758 bool result = false;
1759 enum rtx_code code;
1760 const char *fmt;
1761 int i, j;
1763 if (x == NULL_RTX)
1764 return false;
1766 code = GET_CODE (x);
1767 if (code == SUBREG && subreg_p)
1769 rtx subst, inner = SUBREG_REG (x);
1770 /* Transform subreg of constant while we still have inner mode
1771 of the subreg. The subreg internal should not be an insn
1772 operand. */
1773 if (REG_P (inner) && (int) REGNO (inner) == old_regno
1774 && CONSTANT_P (new_reg)
1775 && (subst = simplify_subreg (GET_MODE (x), new_reg, GET_MODE (inner),
1776 SUBREG_BYTE (x))) != NULL_RTX)
1778 *loc = subst;
1779 return true;
1783 else if (code == REG && (int) REGNO (x) == old_regno)
1785 machine_mode mode = GET_MODE (x);
1786 machine_mode inner_mode = GET_MODE (new_reg);
1788 if (mode != inner_mode
1789 && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
1791 if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
1792 || ! SCALAR_INT_MODE_P (inner_mode))
1793 new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
1794 else
1795 new_reg = gen_lowpart_SUBREG (mode, new_reg);
1797 *loc = new_reg;
1798 return true;
1801 /* Scan all the operand sub-expressions. */
1802 fmt = GET_RTX_FORMAT (code);
1803 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1805 if (fmt[i] == 'e')
1807 if (lra_substitute_pseudo (&XEXP (x, i), old_regno,
1808 new_reg, subreg_p))
1809 result = true;
1811 else if (fmt[i] == 'E')
1813 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1814 if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno,
1815 new_reg, subreg_p))
1816 result = true;
1819 return result;
1822 /* Call lra_substitute_pseudo within an insn. Try to simplify subreg
1823 of constant if SUBREG_P. This won't update the insn ptr, just the
1824 contents of the insn. */
1825 bool
1826 lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno,
1827 rtx new_reg, bool subreg_p)
1829 rtx loc = insn;
1830 return lra_substitute_pseudo (&loc, old_regno, new_reg, subreg_p);
1835 /* This page contains code dealing with scratches (changing them onto
1836 pseudos and restoring them from the pseudos).
1838 We change scratches into pseudos at the beginning of LRA to
1839 simplify dealing with them (conflicts, hard register assignments).
1841 If the pseudo denoting scratch was spilled it means that we do need
1842 a hard register for it. Such pseudos are transformed back to
1843 scratches at the end of LRA. */
1845 /* Description of location of a former scratch operand. */
1846 struct sloc
1848 rtx_insn *insn; /* Insn where the scratch was. */
1849 int nop; /* Number of the operand which was a scratch. */
1852 typedef struct sloc *sloc_t;
1854 /* Locations of the former scratches. */
1855 static vec<sloc_t> scratches;
1857 /* Bitmap of scratch regnos. */
1858 static bitmap_head scratch_bitmap;
1860 /* Bitmap of scratch operands. */
1861 static bitmap_head scratch_operand_bitmap;
1863 /* Return true if pseudo REGNO is made of SCRATCH. */
1864 bool
1865 lra_former_scratch_p (int regno)
1867 return bitmap_bit_p (&scratch_bitmap, regno);
1870 /* Return true if the operand NOP of INSN is a former scratch. */
1871 bool
1872 lra_former_scratch_operand_p (rtx_insn *insn, int nop)
1874 return bitmap_bit_p (&scratch_operand_bitmap,
1875 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
1878 /* Register operand NOP in INSN as a former scratch. It will be
1879 changed to scratch back, if it is necessary, at the LRA end. */
1880 void
1881 lra_register_new_scratch_op (rtx_insn *insn, int nop)
1883 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
1884 rtx op = *id->operand_loc[nop];
1885 sloc_t loc = XNEW (struct sloc);
1886 lra_assert (REG_P (op));
1887 loc->insn = insn;
1888 loc->nop = nop;
1889 scratches.safe_push (loc);
1890 bitmap_set_bit (&scratch_bitmap, REGNO (op));
1891 bitmap_set_bit (&scratch_operand_bitmap,
1892 INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
1893 add_reg_note (insn, REG_UNUSED, op);
1896 /* Change scratches onto pseudos and save their location. */
1897 static void
1898 remove_scratches (void)
1900 int i;
1901 bool insn_changed_p;
1902 basic_block bb;
1903 rtx_insn *insn;
1904 rtx reg;
1905 lra_insn_recog_data_t id;
1906 struct lra_static_insn_data *static_id;
1908 scratches.create (get_max_uid ());
1909 bitmap_initialize (&scratch_bitmap, &reg_obstack);
1910 bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
1911 FOR_EACH_BB_FN (bb, cfun)
1912 FOR_BB_INSNS (bb, insn)
1913 if (INSN_P (insn))
1915 id = lra_get_insn_recog_data (insn);
1916 static_id = id->insn_static_data;
1917 insn_changed_p = false;
1918 for (i = 0; i < static_id->n_operands; i++)
1919 if (GET_CODE (*id->operand_loc[i]) == SCRATCH
1920 && GET_MODE (*id->operand_loc[i]) != VOIDmode)
1922 insn_changed_p = true;
1923 *id->operand_loc[i] = reg
1924 = lra_create_new_reg (static_id->operand[i].mode,
1925 *id->operand_loc[i], ALL_REGS, NULL);
1926 lra_register_new_scratch_op (insn, i);
1927 if (lra_dump_file != NULL)
1928 fprintf (lra_dump_file,
1929 "Removing SCRATCH in insn #%u (nop %d)\n",
1930 INSN_UID (insn), i);
1932 if (insn_changed_p)
1933 /* Because we might use DF right after caller-saves sub-pass
1934 we need to keep DF info up to date. */
1935 df_insn_rescan (insn);
1939 /* Changes pseudos created by function remove_scratches onto scratches. */
1940 static void
1941 restore_scratches (void)
1943 int regno;
1944 unsigned i;
1945 sloc_t loc;
1946 rtx_insn *last = NULL;
1947 lra_insn_recog_data_t id = NULL;
1949 for (i = 0; scratches.iterate (i, &loc); i++)
1951 if (last != loc->insn)
1953 last = loc->insn;
1954 id = lra_get_insn_recog_data (last);
1956 if (REG_P (*id->operand_loc[loc->nop])
1957 && ((regno = REGNO (*id->operand_loc[loc->nop]))
1958 >= FIRST_PSEUDO_REGISTER)
1959 && lra_get_regno_hard_regno (regno) < 0)
1961 /* It should be only case when scratch register with chosen
1962 constraint 'X' did not get memory or hard register. */
1963 lra_assert (lra_former_scratch_p (regno));
1964 *id->operand_loc[loc->nop]
1965 = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
1966 lra_update_dup (id, loc->nop);
1967 if (lra_dump_file != NULL)
1968 fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
1969 INSN_UID (loc->insn), loc->nop);
1972 for (i = 0; scratches.iterate (i, &loc); i++)
1973 free (loc);
1974 scratches.release ();
1975 bitmap_clear (&scratch_bitmap);
1976 bitmap_clear (&scratch_operand_bitmap);
1981 /* Function checks RTL for correctness. If FINAL_P is true, it is
1982 done at the end of LRA and the check is more rigorous. */
1983 static void
1984 check_rtl (bool final_p)
1986 basic_block bb;
1987 rtx_insn *insn;
1989 lra_assert (! final_p || reload_completed);
1990 FOR_EACH_BB_FN (bb, cfun)
1991 FOR_BB_INSNS (bb, insn)
1992 if (NONDEBUG_INSN_P (insn)
1993 && GET_CODE (PATTERN (insn)) != USE
1994 && GET_CODE (PATTERN (insn)) != CLOBBER
1995 && GET_CODE (PATTERN (insn)) != ASM_INPUT)
1997 if (final_p)
1999 extract_constrain_insn (insn);
2000 continue;
2002 /* LRA code is based on assumption that all addresses can be
2003 correctly decomposed. LRA can generate reloads for
2004 decomposable addresses. The decomposition code checks the
2005 correctness of the addresses. So we don't need to check
2006 the addresses here. Don't call insn_invalid_p here, it can
2007 change the code at this stage. */
2008 if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
2009 fatal_insn_not_found (insn);
2013 /* Determine if the current function has an exception receiver block
2014 that reaches the exit block via non-exceptional edges */
2015 static bool
2016 has_nonexceptional_receiver (void)
2018 edge e;
2019 edge_iterator ei;
2020 basic_block *tos, *worklist, bb;
2022 /* If we're not optimizing, then just err on the safe side. */
2023 if (!optimize)
2024 return true;
2026 /* First determine which blocks can reach exit via normal paths. */
2027 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
2029 FOR_EACH_BB_FN (bb, cfun)
2030 bb->flags &= ~BB_REACHABLE;
2032 /* Place the exit block on our worklist. */
2033 EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
2034 *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
2036 /* Iterate: find everything reachable from what we've already seen. */
2037 while (tos != worklist)
2039 bb = *--tos;
2041 FOR_EACH_EDGE (e, ei, bb->preds)
2042 if (e->flags & EDGE_ABNORMAL)
2044 free (worklist);
2045 return true;
2047 else
2049 basic_block src = e->src;
2051 if (!(src->flags & BB_REACHABLE))
2053 src->flags |= BB_REACHABLE;
2054 *tos++ = src;
2058 free (worklist);
2059 /* No exceptional block reached exit unexceptionally. */
2060 return false;
2064 /* Process recursively X of INSN and add REG_INC notes if necessary. */
2065 static void
2066 add_auto_inc_notes (rtx_insn *insn, rtx x)
2068 enum rtx_code code = GET_CODE (x);
2069 const char *fmt;
2070 int i, j;
2072 if (code == MEM && auto_inc_p (XEXP (x, 0)))
2074 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
2075 return;
2078 /* Scan all X sub-expressions. */
2079 fmt = GET_RTX_FORMAT (code);
2080 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2082 if (fmt[i] == 'e')
2083 add_auto_inc_notes (insn, XEXP (x, i));
2084 else if (fmt[i] == 'E')
2085 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2086 add_auto_inc_notes (insn, XVECEXP (x, i, j));
2091 /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
2092 We change pseudos by hard registers without notification of DF and
2093 that can make the notes obsolete. DF-infrastructure does not deal
2094 with REG_INC notes -- so we should regenerate them here. */
2095 static void
2096 update_inc_notes (void)
2098 rtx *pnote;
2099 basic_block bb;
2100 rtx_insn *insn;
2102 FOR_EACH_BB_FN (bb, cfun)
2103 FOR_BB_INSNS (bb, insn)
2104 if (NONDEBUG_INSN_P (insn))
2106 pnote = &REG_NOTES (insn);
2107 while (*pnote != 0)
2109 if (REG_NOTE_KIND (*pnote) == REG_DEAD
2110 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2111 || REG_NOTE_KIND (*pnote) == REG_INC)
2112 *pnote = XEXP (*pnote, 1);
2113 else
2114 pnote = &XEXP (*pnote, 1);
2117 if (AUTO_INC_DEC)
2118 add_auto_inc_notes (insn, PATTERN (insn));
2122 /* Set to 1 while in lra. */
2123 int lra_in_progress;
2125 /* Start of pseudo regnos before the LRA. */
2126 int lra_new_regno_start;
2128 /* Start of reload pseudo regnos before the new spill pass. */
2129 int lra_constraint_new_regno_start;
2131 /* Avoid spilling pseudos with regno more than the following value if
2132 it is possible. */
2133 int lra_bad_spill_regno_start;
2135 /* Inheritance pseudo regnos before the new spill pass. */
2136 bitmap_head lra_inheritance_pseudos;
2138 /* Split regnos before the new spill pass. */
2139 bitmap_head lra_split_regs;
2141 /* Reload pseudo regnos before the new assignmnet pass which still can
2142 be spilled after the assinment pass as memory is also accepted in
2143 insns for the reload pseudos. */
2144 bitmap_head lra_optional_reload_pseudos;
2146 /* Pseudo regnos used for subreg reloads before the new assignment
2147 pass. Such pseudos still can be spilled after the assinment
2148 pass. */
2149 bitmap_head lra_subreg_reload_pseudos;
2151 /* File used for output of LRA debug information. */
2152 FILE *lra_dump_file;
2154 /* True if we should try spill into registers of different classes
2155 instead of memory. */
2156 bool lra_reg_spill_p;
2158 /* Set up value LRA_REG_SPILL_P. */
2159 static void
2160 setup_reg_spill_flag (void)
2162 int cl, mode;
2164 if (targetm.spill_class != NULL)
2165 for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
2166 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
2167 if (targetm.spill_class ((enum reg_class) cl,
2168 (machine_mode) mode) != NO_REGS)
2170 lra_reg_spill_p = true;
2171 return;
2173 lra_reg_spill_p = false;
2176 /* True if the current function is too big to use regular algorithms
2177 in LRA. In other words, we should use simpler and faster algorithms
2178 in LRA. It also means we should not worry about generation code
2179 for caller saves. The value is set up in IRA. */
2180 bool lra_simple_p;
2182 /* Major LRA entry function. F is a file should be used to dump LRA
2183 debug info. */
2184 void
2185 lra (FILE *f)
2187 int i;
2188 bool live_p, scratch_p, inserted_p;
2190 lra_dump_file = f;
2192 timevar_push (TV_LRA);
2194 /* Make sure that the last insn is a note. Some subsequent passes
2195 need it. */
2196 emit_note (NOTE_INSN_DELETED);
2198 COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
2200 init_reg_info ();
2201 expand_reg_info ();
2203 init_insn_recog_data ();
2205 /* Some quick check on RTL generated by previous passes. */
2206 if (flag_checking)
2207 check_rtl (false);
2209 lra_in_progress = 1;
2211 lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
2212 lra_assignment_iter = lra_assignment_iter_after_spill = 0;
2213 lra_inheritance_iter = lra_undo_inheritance_iter = 0;
2214 lra_rematerialization_iter = 0;
2216 setup_reg_spill_flag ();
2218 /* Function remove_scratches can creates new pseudos for clobbers --
2219 so set up lra_constraint_new_regno_start before its call to
2220 permit changing reg classes for pseudos created by this
2221 simplification. */
2222 lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
2223 lra_bad_spill_regno_start = INT_MAX;
2224 remove_scratches ();
2225 scratch_p = lra_constraint_new_regno_start != max_reg_num ();
2227 /* A function that has a non-local label that can reach the exit
2228 block via non-exceptional paths must save all call-saved
2229 registers. */
2230 if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
2231 crtl->saves_all_registers = 1;
2233 if (crtl->saves_all_registers)
2234 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2235 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
2236 df_set_regs_ever_live (i, true);
2238 /* We don't DF from now and avoid its using because it is to
2239 expensive when a lot of RTL changes are made. */
2240 df_set_flags (DF_NO_INSN_RESCAN);
2241 lra_constraint_insn_stack.create (get_max_uid ());
2242 lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
2243 bitmap_clear (lra_constraint_insn_stack_bitmap);
2244 lra_live_ranges_init ();
2245 lra_constraints_init ();
2246 lra_curr_reload_num = 0;
2247 push_insns (get_last_insn (), NULL);
2248 /* It is needed for the 1st coalescing. */
2249 bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
2250 bitmap_initialize (&lra_split_regs, &reg_obstack);
2251 bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
2252 bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
2253 live_p = false;
2254 if (get_frame_size () != 0 && crtl->stack_alignment_needed)
2255 /* If we have a stack frame, we must align it now. The stack size
2256 may be a part of the offset computation for register
2257 elimination. */
2258 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
2259 lra_init_equiv ();
2260 for (;;)
2262 for (;;)
2264 /* We should try to assign hard registers to scratches even
2265 if there were no RTL transformations in
2266 lra_constraints. */
2267 if (! lra_constraints (lra_constraint_iter == 0)
2268 && (lra_constraint_iter > 1
2269 || (! scratch_p && ! caller_save_needed)))
2270 break;
2271 /* Constraint transformations may result in that eliminable
2272 hard regs become uneliminable and pseudos which use them
2273 should be spilled. It is better to do it before pseudo
2274 assignments.
2276 For example, rs6000 can make
2277 RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
2278 to use a constant pool. */
2279 lra_eliminate (false, false);
2280 /* Do inheritance only for regular algorithms. */
2281 if (! lra_simple_p)
2283 if (flag_ipa_ra)
2285 if (live_p)
2286 lra_clear_live_ranges ();
2287 /* As a side-effect of lra_create_live_ranges, we calculate
2288 actual_call_used_reg_set, which is needed during
2289 lra_inheritance. */
2290 lra_create_live_ranges (true, true);
2291 live_p = true;
2293 lra_inheritance ();
2295 if (live_p)
2296 lra_clear_live_ranges ();
2297 /* We need live ranges for lra_assign -- so build them. But
2298 don't remove dead insns or change global live info as we
2299 can undo inheritance transformations after inheritance
2300 pseudo assigning. */
2301 lra_create_live_ranges (true, false);
2302 live_p = true;
2303 /* If we don't spill non-reload and non-inheritance pseudos,
2304 there is no sense to run memory-memory move coalescing.
2305 If inheritance pseudos were spilled, the memory-memory
2306 moves involving them will be removed by pass undoing
2307 inheritance. */
2308 if (lra_simple_p)
2309 lra_assign ();
2310 else
2312 bool spill_p = !lra_assign ();
2314 if (lra_undo_inheritance ())
2315 live_p = false;
2316 if (spill_p)
2318 if (! live_p)
2320 lra_create_live_ranges (true, true);
2321 live_p = true;
2323 if (lra_coalesce ())
2324 live_p = false;
2326 if (! live_p)
2327 lra_clear_live_ranges ();
2330 /* Don't clear optional reloads bitmap until all constraints are
2331 satisfied as we need to differ them from regular reloads. */
2332 bitmap_clear (&lra_optional_reload_pseudos);
2333 bitmap_clear (&lra_subreg_reload_pseudos);
2334 bitmap_clear (&lra_inheritance_pseudos);
2335 bitmap_clear (&lra_split_regs);
2336 if (! live_p)
2338 /* We need full live info for spilling pseudos into
2339 registers instead of memory. */
2340 lra_create_live_ranges (lra_reg_spill_p, true);
2341 live_p = true;
2343 /* We should check necessity for spilling here as the above live
2344 range pass can remove spilled pseudos. */
2345 if (! lra_need_for_spills_p ())
2346 break;
2347 /* Now we know what pseudos should be spilled. Try to
2348 rematerialize them first. */
2349 if (lra_remat ())
2351 /* We need full live info -- see the comment above. */
2352 lra_create_live_ranges (lra_reg_spill_p, true);
2353 live_p = true;
2354 if (! lra_need_for_spills_p ())
2355 break;
2357 lra_spill ();
2358 /* Assignment of stack slots changes elimination offsets for
2359 some eliminations. So update the offsets here. */
2360 lra_eliminate (false, false);
2361 lra_constraint_new_regno_start = max_reg_num ();
2362 if (lra_bad_spill_regno_start == INT_MAX
2363 && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
2364 && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
2365 /* After switching off inheritance and rematerialization
2366 passes, avoid spilling reload pseudos will be created to
2367 prevent LRA cycling in some complicated cases. */
2368 lra_bad_spill_regno_start = lra_constraint_new_regno_start;
2369 lra_assignment_iter_after_spill = 0;
2371 restore_scratches ();
2372 lra_eliminate (true, false);
2373 lra_final_code_change ();
2374 lra_in_progress = 0;
2375 if (live_p)
2376 lra_clear_live_ranges ();
2377 lra_live_ranges_finish ();
2378 lra_constraints_finish ();
2379 finish_reg_info ();
2380 sbitmap_free (lra_constraint_insn_stack_bitmap);
2381 lra_constraint_insn_stack.release ();
2382 finish_insn_recog_data ();
2383 regstat_free_n_sets_and_refs ();
2384 regstat_free_ri ();
2385 reload_completed = 1;
2386 update_inc_notes ();
2388 inserted_p = fixup_abnormal_edges ();
2390 /* We've possibly turned single trapping insn into multiple ones. */
2391 if (cfun->can_throw_non_call_exceptions)
2393 sbitmap blocks;
2394 blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
2395 bitmap_ones (blocks);
2396 find_many_sub_basic_blocks (blocks);
2397 sbitmap_free (blocks);
2400 if (inserted_p)
2401 commit_edge_insertions ();
2403 /* Replacing pseudos with their memory equivalents might have
2404 created shared rtx. Subsequent passes would get confused
2405 by this, so unshare everything here. */
2406 unshare_all_rtl_again (get_insns ());
2408 if (flag_checking)
2409 check_rtl (true);
2411 timevar_pop (TV_LRA);
2414 /* Called once per compiler to initialize LRA data once. */
2415 void
2416 lra_init_once (void)
2418 init_insn_code_data_once ();
2421 /* Called once per compiler to finish LRA data which are initialize
2422 once. */
2423 void
2424 lra_finish_once (void)
2426 finish_insn_code_data_once ();